[英]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.