OutOfMemoryException while uploading file with System.Net.WebClient

1.7k Views Asked by At

I want to upload a file with http post. The following method works fine but with files >1GB I get a OutOfMemoryExceptions

I found some solutions based on AllowWriteStreamBuffering and System.Net.WebRequest but that doesn't seem help be in this case because I need to solve it with System.Net.WebClient.

The memory usage of my application when the exception is thrown is always about ~500MB

string file = @"C:\test.zip";
string url = @"http://foo.bar";
using (System.Net.WebClient client = new System.Net.WebClient())
{
    using (System.IO.Stream fileStream = System.IO.File.OpenRead(file))
    {
        using (System.IO.Stream requestStream = client.OpenWrite(new Uri(url), "POST"))
        {
            byte[] buffer = new byte[16 * 1024];
            int bytesRead;
            while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) > 0)
            {
                requestStream.Write(buffer, 0, bytesRead);
            }
        }
    }
}

What do I need to change to avoid this error?

1

There are 1 best solutions below

0
On BEST ANSWER

After 1 Day of trying I found a solution for this issue.

Maybe this will help some future visitors

string file = @"C:\test.zip";
string url = @"http://foo.bar";
using (System.IO.Stream fileStream = System.IO.File.OpenRead(file))
{
    using (ExtendedWebClient client = new ExtendedWebClient(fileStream.Length))
    {
        using (System.IO.Stream requestStream = client.OpenWrite(new Uri(url), "POST"))
        {
            byte[] buffer = new byte[16 * 1024];
            int bytesRead;
            while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) > 0)
            {
                requestStream.Write(buffer, 0, bytesRead);
            }
        }
    }
}

Extended WebClient method

private class ExtendedWebClient : System.Net.WebClient
{
    public long ContentLength { get; set; }
    public ExtendedWebClient(long contentLength)
    {
        ContentLength = contentLength;
    }

    protected override System.Net.WebRequest GetWebRequest(Uri uri)
    {
        System.Net.HttpWebRequest hwr = (System.Net.HttpWebRequest)base.GetWebRequest(uri);
        hwr.AllowWriteStreamBuffering = false; //do not load the whole file into RAM
        hwr.ContentLength = ContentLength;
        return (System.Net.WebRequest)hwr;
    }
}