views:

467

answers:

2

I am downloading a file from the server/database via aspx page. When using the content-disposition inline the document opens in correct application but the file name is the same as the web page. I want the document to open in say MS Word but with the correct file name. Here is the code that I am using

Response.Buffer = true; Response.ClearContent(); Response.ClearHeaders(); Response.Clear(); Response.ContentType = MimeType(fileName); //function to return the correct MIME TYPE Response.AddHeader("Content-Disposition", @"inline;filename=" + fileName); Response.AddHeader("Content-Length", image.Length.ToString()); Response.BinaryWrite(image); Response.Flush(); Response.Close();

So again, I want the file to open in MS Word with the correct document file name so that the user can properly save/view.

Ideas? thanks

+1  A: 

Here is the ASP.NET code I use (using Delphi.NET, but you get the idea), which has been working fine for me. Notice that I am setting the Content-Disposition header to attachment instead of inline:

Response.Clear();
Response.Expires = 0;
Response.ContentType := 'content type here';
Response.AppendHeader('Content-Disposition', 'attachment; filename="' + Path.GetFileName('file name here') + '"');
Response.AppendHeader('Content-Length', file size here);

My actual code is a little more complex, as I also support the Content-Encoding response header for clients that support gzip and deflate compression via the Accept-Encoding request header.

Remy Lebeau - TeamB
Thanks...yes this is similar to what I am doing now until I find a better solution. It seems switching to content-disposition - attachment was alternative but still not preferred approach.
David
Why is using "Content-Disposition: attachment" not preferred? That is what most servers use to orce a download prompt.
Remy Lebeau - TeamB
As I understand, with attachment the end user always gets prompted open/save vs. just opening inline within the browser.
David
I notice that you are not including whitespace between 'inline;' and 'filename'. Perhaps some browsers are sensitive to that, and this cannot find the 'filename' attribute correctly?
Remy Lebeau - TeamB
A: 

Make sure that your file name has proper extension with your MIME type.

I.e. if you returning word document, then the file name should ends with ".doc"

Of course, you may use other extension but, in this case, different browsers will treat the attachment in a different way.

Also, make sure that your file name does not contain characters that are not allowed in the file name.

Andrey Tagaew