C# 使用 Firefox 通过 https 下载文件问题
当我尝试下载存储在 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();
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();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我无法对 ProNeticas 的帖子发表评论,因此:
“application/word”不是公认的 mime 类型,尤其是 .docx,而且我怀疑浏览器是否知道如何处理它。
.docx 的正确 mime 类型是
application/vnd.openxmlformats-officedocument.wordprocessingml.document
,他已有的 mime 类型对于 .doc 是正确的,请参阅 Office Mime 类型
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 .docSee Office Mime Types
试试这个...
Try this...