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

时间:2012-01-15 20:04:47

标签: c# asp.net stream filestream ioexception

我使用以下代码将文件发送到外部服务..它适用于小文件,但会抛出大文件的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();
    }
}

1 个答案:

答案 0 :(得分:3)

你得到这个的原因是目的地没有配置为允许大文件。您必须调整目标的最大请求长度的配置。