问题描述
如果可能的话,我想执行文件下载并从控制器级别触发浏览器的文件保存对话框。 (以隐藏文件的位置)
I would like to perform a file download and trigger a browser's file save dialog box from the Controller level, if possible. (to hide a file's location)
我基于我的解决方案中的这篇文章。
I was basing on this article in my solution.
我最后得到的是文件类型(文件路径正确且文件以及),但不会触发浏览器打开对话框来保存此文件。 (在href链接的情况下会发生什么情况)
What I get at the end is a file type(file path is correct and file type as well), but the browser is not triggered to open the dialog box to save this file. (what happens in case of the href link)
如何实现此目标?
推荐答案
您可以执行以下操作。
public IActionResult Download(string filename)
{
var file = Path.Combine(_hostingEnvironment.WebRootPath, "files", filename);
return File(System.IO.File.ReadAllBytes(file), "application/octet-stream", filename);
}
它正在从文件中读取所有字节,并返回 application / octet-stream
,显示下载对话框。
It is reading all bytes from file and returning with application/octet-stream
, which shows the download dialog box.
提到的其他方式
这篇关于.NET Core 2.0 ASP.NET MVC下载文件并调用文件保存对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!