Downloading file from server (asp.net) to IE8 with proper Content-Disposition and file name
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开发者_JS百科;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
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.
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.
精彩评论