无法找到资源

时间:2016-05-02 05:14:21

标签: c# asp.net-mvc-4 fileresult

我有以下操作不返回视图

  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>

我也将其更改为以下链接

   @Html.ActionLink("Download zip", "Download", new { id=@ViewBag.id, @class = "btn" })

 <a href="@Url.Action("download","Home" , new { id=@ViewBag.id})" class="btn"> Download zip</a>

但仍然出错!

  

无法找到资源。

     

描述:HTTP 404.您正在查找的资源(或其中一个依赖项)可能已被删除,名称已更改或暂时不可用。请查看以下网址,确保拼写正确。

     

请求的网址:/ Home / Download / 7

我该怎么办?这个错误情况下动作不返回视图?

1 个答案:

答案 0 :(得分: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);
}