2016-05-02 169 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

当方法从未被击中时,改变代码可能会产生什么样的差异! –