Search code examples
c#asp.netweb-configwebclient

Unable to upload large file using WebClient class in the ASP.NET C# web application


I need to upload a file to virus checking site using this code

public string Scan(string file)
{
    var v = new NameValueCollection();
    v.Add("key", APIKey);
    var c = new MyWebClient() { QueryString = v };
    
    c.Headers.Add("Content-type", "binary/octet-stream");
    ServicePointManager.SecurityProtocol = (SecurityProtocolType)768 | (SecurityProtocolType)3072;
    byte[] b = c.UploadFile(scan, "POST", file);
    string json = Encoding.Default.GetString(b);
    var r = ParseJSON(json);
    if (r.ContainsKey("scan_id"))
    {
        return r["scan_id"];
    }
    throw new Exception(r["result"]);
}

This code works fine with smaller files, but once I try to upload files of 50 MB size I get the exception

The remote server returned an error: (413) Request Entity Too Large.

I did lookup this error and tried increase the maxAllowedContentLength and maxRequestLength values to 2147483647

It didn't help. No matter what I do, I always get the error mentioned above

Is there any way to upload the large file to a site using the WebClient class? Is there any other way to upload the larger file in the ASP.NET Web Forms?

Thank you very much in advance


Solution

  • Do you have control over the 'remote server'? If not, you have no way to override what its maximum accepted file size is. This value will be configured on the receiving (remote) side.