[英]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.