简体   繁体   中英

From Wcf streaming service to directly to disk

I have got a wcf service that sends me streams (large ones usually). As the client application my role is to get a stream over WCF and save it to disk. I've written some code but it seems like first getting the stream into ram and then write it to disk from ram. I want to safely get the stream and writing it directly to disk while not filling the ram with huge files. What is the good way of doing this? Here is what I did until now:

Stream sourceStream = SsClient.GetFile(FolderId, Helper.GetISession());
using (var targetStream = new FileStream(thisComputerPath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None))
{
    //read from the input stream in 65000 byte chunks
    const int bufferLen = 65000;
    var buffer = new byte[bufferLen];
    int count;
    while ((count = sourceStream.Read(buffer, 0, bufferLen)) > 0)
    {
        // save to output stream
        targetStream.Write(buffer, 0, count);
    }
    targetStream.Close();
    sourceStream.Close();
}

I hope I could explain my problem clear enough. Excuse me for my english by the way.

I don't mind using ram for buffering purposes or something like that, i just don't want it to be filled with 1-2 gb of streams each time as it would give clients computer hard times if it just has 2 gb of ram.

Did you check the following posts

How to Save a Stream

and

Writing large stream to a file

Let us know incase of any queries on these implementations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM