如何使用mvc或javascript下载另存为对话框的文件

时间:2018-08-27 06:27:43

标签: javascript jquery asp.net-mvc

在我的项目中,我给出了下载文件的选项。所有文件都存在于服务器上的文件夹中。我已经使用定位标记

完成了此操作
<a class="nav-link" href="/images/myfile.jpg" download >Download</a>

默认情况下,它会将文件下载到下载文件夹中,但我想另存为对话框,询问在哪里下载/保存文件。

在C#中,我已经尝试过了。以下代码还将下载文件下载到下载文件夹中

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();
}

1 个答案:

答案 0 :(得分:0)

您可以直接使用return View()直接返回文件,而不必使用return File()

public ActionResult index()
{
    string path = Server.MapPath("~/images/121.jpg");
    string contentType = "image/jpeg";

    return File(path, contentType, "121.jpg");
}

请注意,保存对话框窗口取决于客户端的浏览器设置,您不能从服务器端对其进行控制。如果客户端浏览器启用了自动下载到指定文件夹的功能,则文件将下载到给定的文件夹名称中。

相关问题