使用FileResult MVC4下载/打开Excel文件不起作用

时间:2014-03-27 21:43:25

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

我正在尝试使用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");
    }

1 个答案:

答案 0 :(得分:6)

检查一下:

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

Extra info