【问题标题】:Downloading/Opening Excel File with FileResult MVC4 Not Working使用 FileResult MVC4 下载/打开 Excel 文件不工作
【发布时间】:2014-03-27 21:43:25
【问题描述】:

我正在尝试使用 FileResult 对象在 Excel 中打开本地磁盘上的 excel 文件。当我单击文件打开时,它会下载与我的 ActionResult(WTF?)命名相同的文件,当我单击下载的文件时,它会弹出“选择程序”窗口。如果我选择 Excel,它会打开它,但是我缺少什么让它下载为 Excel 文件并在没有额外步骤的情况下打开它?下面是我打开文件的 switch 语句。谢谢

public ActionResult GetFile(string path)
    {

        string extension = new FileInfo(path).Extension;
        if (extension != null || extension != string.Empty)
        {
            switch (extension)
            {
                case ".pdf":
                    return File(path, "application/pdf");
                case ".txt":
                    return File(path, "application/plain");
                case ".jpeg":
                    return File(path, "application/jpeg");
                case ".doc":
                    return File(path, "application/msword");
                case ".docx":
                    return File(path, "application/msword");
                case ".xls":
                    return File(path, "application/msexcel");
                case ".xlsx":
                    return File(path, "application/msexcel");
                default:
                    return File(path, "application/octet-stream");
            }
        }
        return View("Index");
    }

【问题讨论】:

  • 其他文件类型能用吗?
  • 是的,只有 excel 有问题。将尝试以下解决方案并返回回复或标记它是否有效。

标签: asp.net-mvc-4 file-io fileresult


【解决方案1】:

检查一下:

 case ".xls":
    return File(path, "application/vnd.ms-excel");
 case ".xlsx":
   return File(path, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");

Extra info

【讨论】:

    猜你喜欢
    • 2013-08-18
    • 1970-01-01
    • 1970-01-01
    • 2019-01-27
    • 2017-08-23
    • 2015-02-15
    • 1970-01-01
    • 1970-01-01
    • 2017-05-09
    相关资源
    最近更新 更多