[英]How to download file with save as dialog using mvc or javascript
在我的项目中,我给出了下载文件的选项。 所有文件都存在于服务器上的文件夹中。 我已经使用锚标记完成了
<a class="nav-link" href="/images/myfile.jpg" download >Download</a>
默认情况下,它会将文件下载到下载文件夹中,但我想另存为对话框,以询问在哪里下载/保存文件。
在C#中,我已经尝试过了。 以下代码也下载了downloads文件夹中的文件
public ActionResult index()
{
Response.ContentType = "image/jpeg";
Response.AppendHeader("Content-Disposition", "attachment; filename=121.jpg");
Response.TransmitFile(Server.MapPath("~/images/121.jpg"));
Response.End();
return View();
}
您可以直接使用return File()
直接返回文件,而不必使用return View()
return File()
:
public ActionResult index()
{
string path = Server.MapPath("~/images/121.jpg");
string contentType = "image/jpeg";
return File(path, contentType, "121.jpg");
}
请注意,“ 保存对话框”窗口取决于客户端的浏览器设置 ,您不能从服务器端对其进行控制。 如果客户端浏览器启用了自动下载到指定文件夹的功能,则文件将下载到给定的文件夹名称中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.