【问题标题】:File Download issue in FireFox only仅在 FireFox 中的文件下载问题
【发布时间】:2012-04-18 22:12:09
【问题描述】:

我已经制作了一个消息系统,用户可以在其中相互发送消息,他们也可以在消息中将文件作为附件发送(就像简单的电子邮件系统一样)。 如果文件名包含空格(例如,ticket.doc 的 602_Sign 文件),我在 Firefox 中遇到问题 在 Firefox 中它会使用 602_Sign.doc 保存,但是它应该显示完整的名称,问题在 IE 和 chrome 上运行良好,下面是我下载文件的代码

public ActionResult Download(string attFileName)
        {
            string FileName = Path.Combine(Server.MapPath("~/MessageAttachmentFiles"), attFileName);
            System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
            response.ClearContent();
            response.Clear();
            Response.AddHeader("Content-Disposition", string.Format("attachment; filename = {0}", System.IO.Path.GetFileName(FileName)));
            response.TransmitFile(FileName);
            response.Flush();
            response.End();
            return null;

        }

【问题讨论】:

    标签: c# cross-browser http-headers


    【解决方案1】:

    以下应该可以工作

    response.AddHeader("Content-Disposition", 
                        string.Format("attachment; filename = \"{0}\"",
                        System.IO.Path.GetFileName(FileName)));
    

    更多关于为什么http://kb.mozillazine.org/Filenames_with_spaces_are_truncated_upon_download的细节

    【讨论】:

    • 请注意,文件名封装在双引号内。这将确保正确发送文件名。
    • 谢谢,添加 \"{0}\" 在 Firefox 上修复了它。注意:Chrome 和 IE 很好用。我的代码作为示例(我希望它有所帮助): Response.AddHeader("Content-Disposition",string.Format("attachment;filename=\"{0}\"",Path.GetFileName(filePath)));跨度>
    【解决方案2】:

    文件名应该用双引号括起来

    Content-Disposition: attachment; filename="602_Sign File for ticket.doc" 
    

    【讨论】:

    • Response.AppendHeader("Content-Disposition", "attachment; filename=" + "\"" + stringFileName + "\"");
    【解决方案3】:
    response.AddHeader("Content-Disposition", 
                        string.Format("attachment; filename = \"{0}\"",
                        System.IO.Path.GetFileName(FileName)));
    

    这是正确的解决方案

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-13
      • 2014-02-21
      相关资源
      最近更新 更多