【问题标题】:Showing download dialog box using iFrame使用 iFrame 显示下载对话框
【发布时间】:2014-09-16 11:56:24
【问题描述】:

我正在使用 EmberJs 开发一个 JavaScript 应用程序。单击按钮后,我使用 Jquery 进行 ajax 调用,此调用返回文件路径。现在在 jquery 的成功功能下,我想将此路径传递给 iframe,它最终应该向我显示下载对话框。

$.ajax({
        type: 'GET',
        url: exportURL,
        success: function(result) {
       //Result contains a value which is abc.xlsx**
       //Now how can i show the download dialog?
    }
  });

这个想法是通过传递文件路径来显示下载对话框。我有什么选择?我不想回发页面。

【问题讨论】:

  • 我不明白你哪里有问题。你不知道如何显示按钮?您不想在 iframe 中重新加载页面吗?下载不是在额外的空白页面上吗?还请包括您的 ajax 代码和使用的 HTML。
  • @Spokey 我能够添加按钮,进行 jquery 调用并能够从服务器获取文件路径。现在我只需要显示下载对话框。如何显示下载对话框?
  • 在大多数情况下,如果您链​​接到该文件,它将自动下载。根据服务器端,您必须设置标头以强制下载。这是一个例子stackoverflow.com/a/3600580/2220391
  • @Spokey:谢谢。它奏效了。

标签: javascript jquery iframe ember.js download


【解决方案1】:

根据 speaky 的建议,我添加了这样的 iframe:

 <iframe id="secretIFrame" src="" style="display:none; visibility:hidden;"></iframe>

在Js端:

$.ajax({
        type: 'GET',
        url: exportURL,
        success: function(result) {
             $('#secretIFrame').attr('src','http://localhost:1234/file/fileName');       

    }
  });

http://localhost:1234/file/fileNameasp.net web api restful service,file 是控制器,它以文件名作为参数,它看起来像这样:

public class FileController : ApiController
    {
        public HttpResponseMessage Get(string id)
        {

            var path = Path.Combine("temp", id);
            HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);

            response.Content = new StreamContent(new FileStream(path, FileMode.Open, FileAccess.ReadWrite));
            response.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
            response.Content.Headers.ContentDisposition.FileName = id;
            return response;

        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 2016-11-23
    • 2012-06-26
    相关资源
    最近更新 更多