WebClient 上传文件的大小限制
Size limit on UploadFile of WebClient
我在将 zip 文件上传到 Azure 以获取网络作业时遇到问题。
当 zip 文件大小小于 10MB 时一切顺利,但是当尝试上传更大的文件时出现此异常:
The stream does not support concurrent IO read or write operations
堆栈:
- at System.Net.ConnectStream.InternalWrite(Boolean async, Byte[] buffer, Int32 offset, Int32 size, AsyncCallback callback, Object state)
- at System.Net.ConnectStream.Write(Byte[] buffer, Int32 offset, Int32 size)
- at System.Net.WebClient.UploadBitsState.WriteBytes()
- at System.Net.WebClient.UploadBits(WebRequest request, Stream readStream, Byte[] buffer, Int32 chunkSize, Byte[] header, Byte[] footer, CompletionDelegate uploadCompletionDelegate, CompletionDelegate downloadCompletionDelegate, AsyncOperation asyncOp)
- at System.Net.WebClient.UploadFile(Uri address, String method, String fileName)
这是我的代码:
var client = new MyWebClient
{
Credentials = new NetworkCredential(webSite.UserName, webSite.Password),
};
client.Headers.Add(HttpRequestHeader.ContentType, "application/zip");
client.Headers.Add("Content-Disposition", $"attachment; filename={appFile}");
var response = client.UploadFile(uploadUri, "PUT", filePath);
其中 MyWebClient
是 WebClient
的实现,我需要在其中设置超时:
private class MyWebClient : WebClient
{
protected override WebRequest GetWebRequest(Uri uri)
{
WebRequest w = base.GetWebRequest(uri);
w.Timeout = 20 * 60 * 1000;
return w;
}
}
知道大小限制在哪里吗?我确定它是客户端,而不依赖于 .Net Framework,因为我对其中的几个进行了测试。
编辑
解决方案是通过将 WebClient 转换为 HttpWebRequest 来强制 AllowWriteStreamBuffering 为 false:
protected override WebRequest GetWebRequest(Uri uri)
{
WebRequest w = base.GetWebRequest(uri);
w.Timeout = 20 * 60 * 1000;
var httpRequest = w as HttpWebRequest;
if (httpRequest != null)
{
httpRequest.AllowWriteStreamBuffering = false;
}
return w;
}
此博客条目 http://vikeed.blogspot.co.uk/2011/03/uploading-large-files-using-http-put-in.html 中记录了此行为,建议添加以下内容;
HttpWebRequest request = new ...
request.AllowWriteStreamBuffering = false;
此处发布了一个相同的问题 ,表明使用 AllowAutoRedirect = false
会导致此错误。
您可以尝试按照这两个类似帖子中的建议修改您的设置,看看它是否可以纠正您的错误。
我在将 zip 文件上传到 Azure 以获取网络作业时遇到问题。
当 zip 文件大小小于 10MB 时一切顺利,但是当尝试上传更大的文件时出现此异常:
The stream does not support concurrent IO read or write operations
堆栈:
- at System.Net.ConnectStream.InternalWrite(Boolean async, Byte[] buffer, Int32 offset, Int32 size, AsyncCallback callback, Object state)
- at System.Net.ConnectStream.Write(Byte[] buffer, Int32 offset, Int32 size)
- at System.Net.WebClient.UploadBitsState.WriteBytes()
- at System.Net.WebClient.UploadBits(WebRequest request, Stream readStream, Byte[] buffer, Int32 chunkSize, Byte[] header, Byte[] footer, CompletionDelegate uploadCompletionDelegate, CompletionDelegate downloadCompletionDelegate, AsyncOperation asyncOp)
- at System.Net.WebClient.UploadFile(Uri address, String method, String fileName)
这是我的代码:
var client = new MyWebClient
{
Credentials = new NetworkCredential(webSite.UserName, webSite.Password),
};
client.Headers.Add(HttpRequestHeader.ContentType, "application/zip");
client.Headers.Add("Content-Disposition", $"attachment; filename={appFile}");
var response = client.UploadFile(uploadUri, "PUT", filePath);
其中 MyWebClient
是 WebClient
的实现,我需要在其中设置超时:
private class MyWebClient : WebClient
{
protected override WebRequest GetWebRequest(Uri uri)
{
WebRequest w = base.GetWebRequest(uri);
w.Timeout = 20 * 60 * 1000;
return w;
}
}
知道大小限制在哪里吗?我确定它是客户端,而不依赖于 .Net Framework,因为我对其中的几个进行了测试。
编辑 解决方案是通过将 WebClient 转换为 HttpWebRequest 来强制 AllowWriteStreamBuffering 为 false:
protected override WebRequest GetWebRequest(Uri uri)
{
WebRequest w = base.GetWebRequest(uri);
w.Timeout = 20 * 60 * 1000;
var httpRequest = w as HttpWebRequest;
if (httpRequest != null)
{
httpRequest.AllowWriteStreamBuffering = false;
}
return w;
}
此博客条目 http://vikeed.blogspot.co.uk/2011/03/uploading-large-files-using-http-put-in.html 中记录了此行为,建议添加以下内容;
HttpWebRequest request = new ...
request.AllowWriteStreamBuffering = false;
此处发布了一个相同的问题 ,表明使用 AllowAutoRedirect = false
会导致此错误。
您可以尝试按照这两个类似帖子中的建议修改您的设置,看看它是否可以纠正您的错误。