当我尝试下载存储在SQL DB中的文件时,我对Firefox只有一个非常奇怪的问题(适用于IE和Chrome)。仅当用户尝试将文件保存在其计算机上时才会出现问题,因为它无法识别文件的扩展名。如果用户尝试打开它并且浏览器能够检测其是否为word,excel或pdf文件,则它可以正常工作。这是我的代码块:
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();
答案 0 :(得分:2)
我不能评论ProNeticas的帖子,所以:
“application / word”不是公认的mime类型,尤其是.docx,我怀疑浏览器会知道如何处理它。
.docx的正确mime类型是application/vnd.openxmlformats-officedocument.wordprocessingml.document
,他已经拥有的mime类型对于.doc是正确的
答案 1 :(得分:0)
试试这个......
Response.AddHeader('Content-type', 'application/msword');
Response.AddHeader('Content-Disposition', 'attachment; filename="file.docx"');