显示使用iFrame的下载对话框

时间:2014-09-16 11:56:25

标签: javascript jquery iframe ember.js download

我正在使用EmberJs开发JavaScript应用程序。点击一个按钮,我使用Jquery& amp;这个调用返回一个文件路径。现在在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?
    }
  });

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

1 个答案:

答案 0 :(得分:0)

根据发言人的建议,我已经添加了这样的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,文件是控制器,它将文件名作为参数,它看起来像这样:

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;

        }
    }
相关问题