[英]Showing download dialog box using iFrame
我正在使用EmberJs开发JavaScript应用程序。 单击按钮后,我使用Jquery进行了ajax调用,此调用返回了一个文件路径。 现在,在jquery的成功功能下,我想将此路径传递给iframe,该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/fileName
是asp.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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.