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