C# 使用 Firefox 通过 https 下载文件问题

发布于 2024-10-21 01:24:41 字数 964 浏览 0 评论 0原文

当我尝试下载存储在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

<逆流佳人身旁 2024-10-28 01:24:41

我无法对 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 .doc

See Office Mime Types

且行且努力 2024-10-28 01:24:41

试试这个...

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

Try this...

Response.AddHeader('Content-type', 'application/msword');
Response.AddHeader('Content-Disposition', 'attachment; filename="file.docx"');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文