I use code below to send file from ASP.NET Rest Api to client. Sometimes I see error logs "The remote host closed the connection. The error code is 0x800703E3." Is this correct method to send stream response?
var outputFileName;
context.Response.ContentType = WebHelper.GetMimeType(outputFileName);
context.Response.AddHeader("Content-Disposition", string.Format("{0}; filename={1}", "attachment", outputFileName));
context.Response.AddHeader("Content-Length", binaryData.Length.ToString());
context.Response.OutputStream.Write(binaryData, 0, binaryData.Length);
context.Response.Flush();
context.Response.Close();
This this:
context.Response.Clear();
context.Response.ContentType = ...;
context.Response.AddHeader( "Content-Disposition", ... );
// do not set Content-Length
context.Response.Write( binaryData );
context.Response.Flush();
context.Response.End();
If you have the data in a file, it is much better to use Response.TransmitFile than Response.OutputStream.Write since TransmitFile will not buffer the file data in memory.
As for the error message that you are receiving, this is perfectly normal. If the user disconnects from the network, cancels the download, or shuts down their browser while the file is being transmitted, this error message will be raised to let you know that the transfer was not completed.
User contributions licensed under CC BY-SA 3.0