ASP Net Core-文件下载结果是FileNotFoundException



我正在尝试下载一个简单的文件,但是它会导致FilenotFoundException。

控制器中的代码:

public FileResult DownloadFile()
{
    var fileName = "1.pdf";
    var filePath = env.WebRootPath + "\" + fileName;
    var fileExists = System.IO.File.Exists(filePath);
    return File(filePath, "application/pdf", fileName);
}

(调试变量文件登记者显示其设置为true。)

视图中的代码:

@Html.ActionLink("Download", "DownloadFile")

来自日志的消息:

2017-03-12 09:28:45 [INF] Executing action method "Team.Controllers.ModulesExController.DownloadFile (Team)" with arguments (null) - ModelState is Valid
2017-03-12 09:28:45 [DBG] Executed action method "Team.Controllers.ModulesExController.DownloadFile (Team)", returned result "Microsoft.AspNetCore.Mvc.VirtualFileResult".
2017-03-12 09:28:45 [INF] Executing FileResult, sending file as "1.pdf"
2017-03-12 09:28:45 [INF] Executed action "Team.Controllers.ModulesExController.DownloadFile (Team)" in 0.8238ms
2017-03-12 09:28:45 [DBG] System.IO.FileNotFoundException occurred, checking if Entity Framework recorded this exception as resulting from a failed database operation.
2017-03-12 09:28:45 [DBG] Entity Framework did not record any exceptions due to failed database operations. This means the current exception is not a failed Entity Framework database operation, or the current exception occurred from a DbContext that was not obtained from request services.
2017-03-12 09:28:45 [ERR] An unhandled exception has occurred while executing the request
System.IO.FileNotFoundException: Could not find file: D:ProjektiTeamsrcTeamwwwroot1.pdf

如果我在浏览器中的错误消息中粘贴了链接,我可以打开文件。

在ASP.NET Core 2.0中,如果您有文件路径,则可以将物理文件用作返回类型。

    public FileResult DownloadFile() {
        var fileName = "1.pdf";
        var filePath = env.WebRootPath + "\" + fileName;
        var fileExists = System.IO.File.Exists(filePath);
        return PhysicalFile(filePath, "application/pdf", fileName);
    }

Controller.File()期望虚拟路径,而不是绝对路径。

如果要使用绝对路径,请传递流:

public FileResult DownloadFile()
{
    var fileName = "1.pdf";
    var filePath = env.WebRootPath + "\" + fileName;
    var fileExists = System.IO.File.Exists(filePath);
    var fs = System.IO.File.OpenRead(filePath);
    return File(fs, "application/pdf", fileName);
}

此代码下载了您已放入MVC 5 Resources文件夹中的名为'abc123.pdf'的PDF文件。我花了几个小时将其他堆栈溢出文章拼接在一起,以使其正常工作。我见过的大多数解决方案都不告诉您如何下载物理文件。大多数解决方案都没有提及如何处理资源文件,我发现它非常有用。

    public IActionResult DownloadBlankACH()
    {
        return DownloadDocumentLikeThis("abc123");
    }
    private IActionResult DownloadDocumentLikeThis(string likeThis)
    {
        ResourceManager MyResourceClass = new ResourceManager(typeof(Resources));
        ResourceSet resourceSet = MyResourceClass.GetResourceSet(CultureInfo.CurrentUICulture, true, true);
        foreach (DictionaryEntry entry in resourceSet)
        {
            string resourceKey = entry.Key.ToString();
            object resource = entry.Value;
            if (resourceKey.Contains(likeThis))
            {
                string RunningPath = AppDomain.CurrentDomain.BaseDirectory;
                string FileName = string.Format("{0}Properties\" + resourceKey + ".pdf", Path.GetFullPath(Path.Combine(RunningPath, @"......")));
                return PhysicalFile(System.IO.Path.GetFullPath(FileName), "application/pdf", System.IO.Path.GetFileName(FileName));
            }
        }

        return View();
    }

相关内容

最新更新