DAK
DAK

Reputation: 1435

C# File download with Firefox issue over https

I have a very weird issue with Firefox only (works fine with IE & Chrome) when I try to download a file stored in SQL DB. Issue only comes up when the user tries to save the file on their machine as it cannot recognize the extension of the file. It works fine if the user tries to open it and browser is able to detect if its a word, excel, or pdf file. Here is my code block:

Attachments attach = AttachmentsSession[e.Item.ItemIndex] as Attachments;
string extension = attach.Extension;
byte[] bytFile = attach.AttachmentData;
string fileName = attach.Name;

Response.ClearHeaders();
Response.Clear();
Response.Buffer = true;

if (extension == ".doc")
{
   Response.ContentType = "application/vnd.ms-word";
   Response.AddHeader("content-disposition", "attachment;filename=" + fileName);
}

else if (extension == ".docx")
{
   Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
    Response.AddHeader("content-disposition", "attachment;filename=" + fileName);
}

Response.Charset = "";
Response.BinaryWrite(bytFile);
HttpContext.Current.ApplicationInstance.CompleteRequest();
Response.End();

Upvotes: 0

Views: 1653

Answers (2)

Brian E
Brian E

Reputation: 164

I can't comment on ProNeticas' post, so:

"application/word" isn't a recognised mime type, least of all for .docx, and I doubt a browser would know what to do with it.

The correct mime type for .docx is application/vnd.openxmlformats-officedocument.wordprocessingml.document, and the mime type he already has is correct for .doc

See Office Mime Types

Upvotes: 2

ProNeticas
ProNeticas

Reputation: 996

Try this...

Response.AddHeader('Content-type', 'application/msword');
Response.AddHeader('Content-Disposition', 'attachment; filename="file.docx"');

Upvotes: 0

Related Questions