HttpException on OutputStream. How to correctly send file

0

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();
c#
asp.net
file
httpresponse
asked on Stack Overflow Dec 5, 2011 by Tomas

2 Answers

0

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();
answered on Stack Overflow Dec 5, 2011 by Wiktor Zychla
0

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.

answered on Stack Overflow Dec 6, 2011 by competent_tech

User contributions licensed under CC BY-SA 3.0