.NET Core 2.0 ASP.NET MVC下载文件并调用文件保存对话框 [英] .NET Core 2.0 ASP.NET MVC Download File and Invoke File Save Dialog Box

查看:246
本文介绍了.NET Core 2.0 ASP.NET MVC下载文件并调用文件保存对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果可能的话,我想执行文件下载并从控制器级别触发浏览器的文件保存对话框。 (以隐藏文件的位置)

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.

https://www.codeproject.com/Articles/1203408 / Upload-Download-Files-in-ASP-NET-Core

我最后得到的是文件类型(文件路径正确且文件以及),但不会触发浏览器打开对话框来保存此文件。 (在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下载文件并调用文件保存对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆