Я использую следующий код для отправки файла во внешнюю службу .. Он работает для небольших файлов, но выдает 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();
Кажется, что-то связано с тем, как я вызываю make the stream ... но я не смог найти точное решение. Подробности ошибок приведены ниже
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();
}
}