I am trying to upload a file to a HTTP URL with WebClient.UploadFile. For small file such as 1M, 2M, the uploading is successful. But for a big file such as 12M, I got this exception:
The request was aborted: The request was canceled.
Has anyone met this problem before and could you share the solution?
Here is the solution referred to in smwikipedia's answer. I've added the ability to disable write stream buffering, which can help with out of memory exceptions.
public class ExtendedWebClient : WebClient
{
public int Timeout { get; set; }
public new bool AllowWriteStreamBuffering { get; set; }
protected override WebRequest GetWebRequest(Uri address)
{
var request = base.GetWebRequest(address);
if (request != null)
{
request.Timeout = Timeout;
var httpRequest = request as HttpWebRequest;
if (httpRequest != null)
{
httpRequest.AllowWriteStreamBuffering = AllowWriteStreamBuffering;
}
}
return request;
}
public ExtendedWebClient()
{
Timeout = 100000; // the standard HTTP Request Timeout default
}
}
Usage:
var webClient = new ExtendedWebClient();
webClient.Timeout = Timeout.Infinite;
webClient.AllowWriteStreamBuffering = false;
webClient.UploadFile(url, filePath);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With