System.IO.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();
}
}
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您收到此消息的原因是目标未配置为允许那么大的文件。您必须调整目标的最大请求长度的配置。
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.