When downloading a file using FileStream, why does page error message refers to aspx page name, not

Posted by StuperUser on Stack Overflow See other posts from Stack Overflow or by StuperUser
Published on 2009-07-07T13:50:31Z Indexed on 2010/04/25 5:23 UTC
Read the original article Hit count: 273

Filed under:
|
|

After building a filepath (path, below) in a string (I am aware of Path in System.IO, but am using someone else's code and do not have the opportunity to refactor it to use Path). I am using a FileStream to deliver the file to the user (see below):

FileStream myStream = new FileStream(path, FileMode.Open, FileAccess.Read);  
long fileSize = myStream.Length;  
byte[] Buffer = new byte[(int)fileSize + 1];  
myStream.Read(Buffer, 0, (int)myStream.Length);  
myStream.Close();  
Response.ContentType = "application/csv";  
Response.AddHeader("content-disposition", "attachment; filename=" + filename);  
Response.BinaryWrite(Buffer);  
Response.Flush();  
Response.End();

I have seen from: http://stackoverflow.com/questions/736301/asp-net-how-to-stream-file-to-user reasons to avoid use of Response.End() and Response.Close(). I have also seen several articles about different ways to transmit files and have diagnosed and found a solution to the problem (https and http headers) with a colleague.

However, the error message that was being displayed was not about access to the file at path, but the aspx file.

Edit: Error message is:

Internet Explorer cannot download MyPage.aspx from server.domain.tld

Internet Explorer was not able to open this Internet site. The requested site is either unavailable or cannot be found. Please try again later. (page name and address anonymised)

Why is this? Is it due to the contents of the file coming from the HTTP response .Flush() method rather than a file being accessed at its address?

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about file-io