使用iFrame显示下载对话框

Showing download dialog box using iFrame

本文关键字:对话框 下载 显示 iFrame 使用      更新时间:2023-09-26

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

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