gpt4 book ai didi

c# - WebClient的UploadFile大小限制

转载 作者:行者123 更新时间:2023-12-03 03:11:16 26 4
gpt4 key购买 nike

我在将 zip 文件上传到 Azure 进行 Web 作业时遇到了问题。

当 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);

哪里MyWebClientWebClient 的实现我需要在其中设置超时:

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;

此处发布了相同的问题 C# HttpClient PUT这表明使用 AllowAutoRedirect = false 会导致此错误。

您可以尝试按照这两篇类似帖子中的建议修改您的设置,看看是否可以纠正您的错误。

关于c# - WebClient的UploadFile大小限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37239297/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com