Writing a file from HttpWebRequest periodically or after the download is complete?

I am currently using this code to download files (with Range header). Most of the files are large and it currently uses 99% of the cpu as a download file. Is there a way that the file can be written periodically so that it doesn't stay in RAM permanently?

private byte[] GetWebPageContent(string url, long start, long finish)
{
    byte[] result = new byte[finish];
    HttpWebRequest request;

    request = WebRequest.Create(url) as HttpWebRequest;

    //request.Headers.Add("Range", "bytes=" + start + "-" + finish);

    request.AddRange((int)start, (int)finish);

    using (WebResponse response = request.GetResponse())
    {
        return ReadFully(response.GetResponseStream());
    }    
}

public static byte[] ReadFully(Stream stream)
{
    byte[] buffer = new byte[32768];
    using (MemoryStream ms = new MemoryStream())
    {
        while (true)
        {
            int read = stream.Read(buffer, 0, buffer.Length);
            if (read <= 0)
                return ms.ToArray();
            ms.Write(buffer, 0, read);
        }
    }
}

      

+2


a source to share


1 answer


Instead of writing data to a MemoryStream (which stores data in memory), write data to a FileStream (which stores data in a file on disk).

byte[] buffer = new byte[32768];
using (FileStream fileStream = File.Create(path))
{
    while (true)
    {
        int read = stream.Read(buffer, 0, buffer.Length);
        if (read <= 0)
            break;
        fileStream.Write(buffer, 0, read);
    }
}

      



Using .NET 4.0:

using (FileStream fileStream = File.Create(path))
{
    stream.CopyTo(fileStream);
}

      

+3


a source







All Articles