System.IO.IOException:在写入所有字节之前无法关闭流

发布于 2024-12-27 06:46:24 字数 2952 浏览 1 评论 0原文

我正在使用以下代码将文件发送到外部服务。它适用于小文件,但对于大文件会抛出 IOException 。

FileStream newStream = File.OpenRead(_fullFilePath);
MemoryStream storeStream = new MemoryStream();
storeStream.SetLength(newStream.Length);
newStream.Read(storeStream.GetBuffer(), 0, (int)newStream.Length);
storeStream.Flush();
//send data to external service
newStream.Close();
storeStream.Close();

这似乎与我调用创建流的方式有关。但我找不到确切的解决方案..下面给出了错误详细信息

System.Net.WebException: The request was aborted: The request was canceled. ---> System.IO.IOException: Cannot close stream until all bytes are written.
  at System.Net.ConnectStream.CloseInternal(Boolean internalCall, Boolean aborting)
  --- End of inner exception stack trace ---
  at System.Net.ConnectStream.CloseInternal(Boolean internalCall, Boolean aborting)
  at System.Net.ConnectStream.System.Net.ICloseEx.CloseEx(CloseExState closeState)
  at System.Net.ConnectStream.Dispose(Boolean disposing)
  at System.IO.Stream.Close()
  at DotNetOpenAuth.Messaging.MessagingUtilities.PostMultipartNoGetResponse(HttpWebRequest request, IDirectWebRequestHandler requestHandler, IEnumerable`1 parts) in c:\Users\andarno\git\dotnetopenid\src\DotNetOpenAuth\Messaging\MessagingUtilities.cs:line 243
  at DotNetOpenAuth.OAuth.ChannelElements.OAuthChannel.InitializeRequestAsAuthHeader(IDirectedProtocolMessage requestMessage) in c:\Users\andarno\git\dotnetopenid\src\DotNetOpenAuth\OAuth\ChannelElements\OAuthChannel.cs:line 373
  at DotNetOpenAuth.OAuth.ChannelElements.OAuthChannel.CreateHttpRequest(IDirectedProtocolMessage request) in c:\Users\andarno\git\dotnetopenid\src\DotNetOpenAuth\OAuth\ChannelElements\OAuthChannel.cs:line 214
  at DotNetOpenAuth.OAuth.ConsumerBase.PrepareAuthorizedRequest(MessageReceivingEndpoint endpoint, String accessToken, IEnumerable`1 binaryData) in c:\Users\andarno\git\dotnetopenid\src\DotNetOpenAuth\OAuth\ConsumerBase.cs:line 132
  at Visual.ApiProvider.DoRequest(MessageReceivingEndpoint message, List`1 parameters) in C:\Users\Nick Bruun\Code\drei\23-api-dotnet\src\Implementations\ApiProvider.cs:line 137
  at Visual.PhotoService.Upload(String filename, String fileContentType, Stream filestream, Nullable`1 userId, Nullable`1 albumId, String title, String description, String tags, Nullable`1 publish) in C:\Users\Nick Bruun\Code\drei\23-api-dotnet\src\Implementations\PhotoService.cs:line 294
  at usercontrols_UploadVideos.Upload23Video(Object parameters) in e:\Projects\videoWeb\usercontrols\UploadVideos.ascx.cs:line 138

新代码与使用

using (FileStream newStream = File.OpenRead(_fullFilePath))
{
    newStream.Flush();
    using (MemoryStream storeStream = new MemoryStream())
    {
        storeStream.SetLength(newStream.Length);
        newStream.Read(storeStream.GetBuffer(), 0, (int)newStream.Length);
        storeStream.Flush();
        newStream.Close();
        //call to webservice
        storeStream.Close();
    }
}

I am using following code to send a file to an external service.. It works for small files, but throws IOException for big files..

FileStream newStream = File.OpenRead(_fullFilePath);
MemoryStream storeStream = new MemoryStream();
storeStream.SetLength(newStream.Length);
newStream.Read(storeStream.GetBuffer(), 0, (int)newStream.Length);
storeStream.Flush();
//send data to external service
newStream.Close();
storeStream.Close();

It seems like something to do with the way I call make the stream.. but I couldnt find the exact solution.. Error details is given below

System.Net.WebException: The request was aborted: The request was canceled. ---> System.IO.IOException: Cannot close stream until all bytes are written.
  at System.Net.ConnectStream.CloseInternal(Boolean internalCall, Boolean aborting)
  --- End of inner exception stack trace ---
  at System.Net.ConnectStream.CloseInternal(Boolean internalCall, Boolean aborting)
  at System.Net.ConnectStream.System.Net.ICloseEx.CloseEx(CloseExState closeState)
  at System.Net.ConnectStream.Dispose(Boolean disposing)
  at System.IO.Stream.Close()
  at DotNetOpenAuth.Messaging.MessagingUtilities.PostMultipartNoGetResponse(HttpWebRequest request, IDirectWebRequestHandler requestHandler, IEnumerable`1 parts) in c:\Users\andarno\git\dotnetopenid\src\DotNetOpenAuth\Messaging\MessagingUtilities.cs:line 243
  at DotNetOpenAuth.OAuth.ChannelElements.OAuthChannel.InitializeRequestAsAuthHeader(IDirectedProtocolMessage requestMessage) in c:\Users\andarno\git\dotnetopenid\src\DotNetOpenAuth\OAuth\ChannelElements\OAuthChannel.cs:line 373
  at DotNetOpenAuth.OAuth.ChannelElements.OAuthChannel.CreateHttpRequest(IDirectedProtocolMessage request) in c:\Users\andarno\git\dotnetopenid\src\DotNetOpenAuth\OAuth\ChannelElements\OAuthChannel.cs:line 214
  at DotNetOpenAuth.OAuth.ConsumerBase.PrepareAuthorizedRequest(MessageReceivingEndpoint endpoint, String accessToken, IEnumerable`1 binaryData) in c:\Users\andarno\git\dotnetopenid\src\DotNetOpenAuth\OAuth\ConsumerBase.cs:line 132
  at Visual.ApiProvider.DoRequest(MessageReceivingEndpoint message, List`1 parameters) in C:\Users\Nick Bruun\Code\drei\23-api-dotnet\src\Implementations\ApiProvider.cs:line 137
  at Visual.PhotoService.Upload(String filename, String fileContentType, Stream filestream, Nullable`1 userId, Nullable`1 albumId, String title, String description, String tags, Nullable`1 publish) in C:\Users\Nick Bruun\Code\drei\23-api-dotnet\src\Implementations\PhotoService.cs:line 294
  at usercontrols_UploadVideos.Upload23Video(Object parameters) in e:\Projects\videoWeb\usercontrols\UploadVideos.ascx.cs:line 138

New code with Using

using (FileStream newStream = File.OpenRead(_fullFilePath))
{
    newStream.Flush();
    using (MemoryStream storeStream = new MemoryStream())
    {
        storeStream.SetLength(newStream.Length);
        newStream.Read(storeStream.GetBuffer(), 0, (int)newStream.Length);
        storeStream.Flush();
        newStream.Close();
        //call to webservice
        storeStream.Close();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谷夏 2025-01-03 06:46:24

您收到此消息的原因是目标未配置为允许那么大的文件。您必须调整目标的最大请求长度的配置。

The reason you're getting this is that the destination isn't configured to allow for files that big. You have to adjust the config of the destination's Max Request length.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文