C# 仅FireFox中的文件下载问题
我制作了一个信息系统,在这个系统中,用户可以互相发送信息,他们也可以在信息中作为附件发送文件(类似于简单的电子邮件系统)。 我在firefox中遇到了一个问题,如果文件名包含空格(例如602_Sign file for ticket.doc) 在firefox中,它将使用602_Sign.doc保存,但它应该显示完整的名称,问题在IE和chrome上运行良好,下面是我下载文件的代码C# 仅FireFox中的文件下载问题,c#,cross-browser,http-headers,C#,Cross Browser,Http Headers,我制作了一个信息系统,在这个系统中,用户可以互相发送信息,他们也可以在信息中作为附件发送文件(类似于简单的电子邮件系统)。 我在firefox中遇到了一个问题,如果文件名包含空格(例如602_Sign file for ticket.doc) 在firefox中,它将使用602_Sign.doc保存,但它应该显示完整的名称,问题在IE和chrome上运行良好,下面是我下载文件的代码 public ActionResult Download(string attFileName)
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;
}
下面的方法应该有效
response.AddHeader("Content-Disposition",
string.Format("attachment; filename = \"{0}\"",
System.IO.Path.GetFileName(FileName)));
有关为什么文件名应该用双引号括起来的详细信息
Content-Disposition: attachment; filename="602_Sign File for ticket.doc"
这是正确的解决方案请注意,文件名封装在双引号中。这将确保文件名正确发送。谢谢,添加\“{0}\”在Firefox上修复了它。注意:Chrome和IE都很好。我的代码就是一个例子(我希望它有帮助):Response.AddHeader(“Content Disposition”,string.Format(“attachment;filename=\“{0}\”,Path.GetFileName(filePath))<代码>响应.AppendHeader(“内容处置”、“附件;文件名=“+”\”“+stringFileName+”\”)代码>
response.AddHeader("Content-Disposition",
string.Format("attachment; filename = \"{0}\"",
System.IO.Path.GetFileName(FileName)));