为什么Firefox修剪响应文件名?

时间:2013-01-20 06:17:52

标签: asp.net-mvc-3 firefox

以下代码访问辅助方法,该方法创建并返回EPPlus ExcelPackage,然后将包返回给浏览器:

public ActionResult DownloadMatrixExcel(int projectId)
        {
            try
            {
                // Get project details
                var project = (from p in db.Projects
                               where p.ProjectId == projectId
                               select new
                               {
                                   companyName = p.Company.Name,
                                   projectName = p.Name
                               }).Single();

                // Must append file type to file download responses
                var fileName = project.projectName + "-" + project.companyName + "-" + DateTime.Now.ToString("yyyyMMdd", CultureInfo.InvariantCulture) + ".xlsx";

                // Configure response
                Response.Clear();
                Response.BufferOutput = false;
                Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                Response.AddHeader("content-disposition", "attachment; filename=" + fileName);

                // Create and populate excel package
                var matrixSpreadsheet = ExcelHelper.BuildMatrixExcel(projectId);
                matrixSpreadsheet.SaveAs(Response.OutputStream);

            }
            catch (Exception e)
            {
                return Content("Error: " + e.Message);
            }

            // Download okay - No ViewResult
            return new EmptyResult();
        } 

在我测试过的每个浏览器中工作正常,但FireFox 18.0.1(尚未测试其他FF版本)修剪第一个空格的文件名,因此“someproject - somecompany - thedate”只是“someproject”。我可以做一个替换和删除空格,但这使得一些文件名看起来有点奇怪。文件扩展名似乎完好无损,没有其他问题,但想知道是否有人可以提供解释或修复?

2 个答案:

答案 0 :(得分:4)

您应该将文件名放在引号字符("filename")之间。

答案 1 :(得分:4)

好的,在研究另一个问题时找到答案:File Download issue in FireFox only

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

当您选择在FireFox中选择保存而不是在浏览器中打开时,这也会为文件提供正确的内容类型。

相关问题