Russ Suter
asked on
ASP.NET Downloading a File
The following code downloads a file in Firefox or IE and then launches the associated program to open the document. In Chrome it just downloads all the binary data into a document named [webpage].aspx rather than opening it with the correct program (Microsoft Excel for example). It does open PDF documents just fine as long as the Chrome PDF Viewer plugin is enabled. If it's disabled it just downloads a .aspx file again just like the others. Is there any way to tell Chrome to use an application to open the file rather than just download it as a misnamed document?
GridItem selectedItem = GridMerchantAppAttachments.SelectedItems[0];
Library.AttachmentRecord attachment = AppSession.CreditApplication.Attachments.FindByName(((GridItem)selectedItem)["FileName"].ToString());
if (attachment.Data == null)
{
attachment.GetData();
}
Response.AppendHeader("Content-Disposition:", String.Format(@"attachment; filename=""{0}""", attachment.FileName));
Response.ContentType = attachment.ContentType;
Response.AppendHeader("Content-Length", attachment.Data.Length.ToString(Thread.CurrentThread.CurrentCulture));
Response.BinaryWrite(attachment.Data);
Response.Flush();
Response.End();
Response.Close();
ASKER
It can vary. Sometimes it's application/pdf, sometimes it's application/msword, sometimes it's audio/wav, sometimes application/vnd.openxmlfor mats-offic edocument. spreadshee tml.sheet, or others.
Actually, I don't believe that there is anything that you can do server-side, since that would open a security hole on the client.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
A solution was discovered (by accident) that seems to have addressed the problem.
Response.ContentType = attachment.ContentType;