【问题标题】:Opening save ,saveas dialog from ajax从ajax打开保存,保存对话框
【发布时间】:2012-07-02 07:34:24
【问题描述】:

当我使用时

window.location = '?fileId=id,

我正在完全不同的屏幕中获取内容

但是如何将其作为打开、保存、另存为对话框之类的弹出窗口打开呢?

我正在点击 ActionResult DownloadFile() 它返回一个 File 对象。

【问题讨论】:

    标签: asp.net-mvc-3 asp.net-ajax


    【解决方案1】:

    您可以使用File 方法的第三个参数来指定文件名。这将具有将Content-Disposition HTTP 响应标头设置为attachment 的效果,当用户导航到此操作时弹出“另存为”对话框:

    public ActionResult DownloadFile(string fileId)
    {
        byte[] file = ...
        // TODO: adjust the MIME type and filename extension to your case accordingly
        return File(file, "text/plain", "foo.txt");
    }
    

    然后:

    var id = '1234';
    window.location.href = '<%= Url.Action("DownloadFile", "Home") %>?fileId=' + id;
    

    会正常工作的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-30
      • 1970-01-01
      相关资源
      最近更新 更多