2014-03-27 38 views
0

我想在Excel中使用FileResult對象在本地磁盤上打開Excel文件。當我點擊文件打開時,它下載與我的ActionResult(WTF?)命名相同的文件,當點擊下載的文件時,它會彈出「選擇程序」窗口。如果我選擇Excel,它會打開它,但我錯過了什麼使它下載作爲Excel文件,並打開它沒有額外的步驟?以下是我打開文件的switch語句。由於下載/打開Excel文件與FileResult MVC4不工作

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"); 
    } 
+0

不要其他文件類型的工作? –

+0

是的,他們只有Excel問題。將嘗試下面的解決方案,並返回一個響應或標記它是否有效。 – user1732364

回答

5

檢查:

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

Extra info

+0

很好用!!!!!欣賞它:D – user1732364