2016-05-02 171 views
0

我有以下動作沒有返回查看資源無法找到

public FileResult Download(int? id) 
    { 
     var files = from p in _db.tbl_Pdfs 
        where p.PaperId == id 
        select p.FileName; 

     var archive = Server.MapPath("~/Content/Zip/archive.zip"); 
     var temp = Server.MapPath("~/Content/Temp"); 

     // clear any existing archive 
     if (System.IO.File.Exists(archive)) 
     { 
      System.IO.File.Delete(archive); 
     } 
     // empty the temp folder 
     Directory.EnumerateFiles(temp).ToList().ForEach(f => System.IO.File.Delete(f)); 

     // copy the selected files to the temp folder 

     foreach (string name in files) 
     { 
      string sourceFile = System.IO.Path.Combine("~/Content/Pdf", name); 
      System.IO.File.Copy(sourceFile, "~/Content/Temp", true); 
     } 
     // create a new archive 
     ZipFile.CreateFromDirectory(temp, archive, CompressionLevel.Fastest, true); 



     return File(archive, "application/zip", "archive.zip"); 
    } 

,也有下面的鏈接下載的zip文件

<a href="~/Home/Download/@ViewBag.id" class="btn">Download zip</a> 

我也將其更改爲以下linkis

@Html.ActionLink("Download zip", "Download", new { [email protected], @class = "btn" }) 

<a href="@Url.Action("download","Home" , new { [email protected]})" class="btn"> Download zip</a> 

但還是給出了一個錯誤!

無法找到該資源。

說明:HTTP 404。您正在尋找(或它的一個依賴)的資源可能已被刪除,更名或暫時不可用。請檢查以下網址並確保它拼寫正確。

請求的URL:/首頁/下載/ 7

我應該怎麼辦?它這個錯誤的情況下,行動不返回一個視圖?

+0

是否'下載()'方法不'HomeController'存在嗎? –

+0

@StephenMuecke當然存在!我認爲這是因爲結果類型和錯誤發生時,沒有找到相關的視圖,但我不知道如何解決它!我不需要行動的觀點! –

+1

錯誤告訴你它不存在。你是否在使用區域(它與'結果類型'無關) –

回答

0

可以請你更改以下,並嘗試

foreach (string name in files) 
{ 
    string sourceFile = Server.MapPath(System.IO.Path.Combine("~/Content/Pdf", name)); 
    System.IO.File.Copy(sourceFile, Server.MapPath(System.IO.Path.Combine("~/Content/Temp", name)), true); 
} 
+0

當方法從未被擊中時,改變代碼可能會產生什麼樣的差異! –