admin管理员组文章数量:1023564
I'm sending a XMLHTTPRequest to a server endpoint using Polymer's iron-ajax element:
<iron-ajax
id="ajax"
method="POST"
url="/export/"
params=''
handle-as="json"
on-response="handleResponse"
</iron-ajax>
My Koa/Express-server responds with a read stream like this:
router.post('/export' , function*(){
var file = __dirname + '/test.zip';
var filename = path.basename(file);
var mimetype = mime.lookup(file);
this.set('Content-disposition', 'attachment; filename=' + filename);
this.set('Content-type', mimetype);
this.body = fs.createReadStream(file);
})
How do I initiate the download in handleResponse()
?
Ideally I don't want to handle the response at all and directly initiate the download.
The response headers look (as expected) like this:
Content-disposition: attachment; filename=test.zip
Connection: keep-alive
Transfer-Encoding: chunked
Content-type: application/zip
I'm sending a XMLHTTPRequest to a server endpoint using Polymer's iron-ajax element:
<iron-ajax
id="ajax"
method="POST"
url="/export/"
params=''
handle-as="json"
on-response="handleResponse"
</iron-ajax>
My Koa/Express-server responds with a read stream like this:
router.post('/export' , function*(){
var file = __dirname + '/test.zip';
var filename = path.basename(file);
var mimetype = mime.lookup(file);
this.set('Content-disposition', 'attachment; filename=' + filename);
this.set('Content-type', mimetype);
this.body = fs.createReadStream(file);
})
How do I initiate the download in handleResponse()
?
Ideally I don't want to handle the response at all and directly initiate the download.
The response headers look (as expected) like this:
Content-disposition: attachment; filename=test.zip
Connection: keep-alive
Transfer-Encoding: chunked
Content-type: application/zip
Share
Improve this question
edited Nov 4, 2015 at 17:37
Hedge
asked Nov 4, 2015 at 17:15
HedgeHedge
16.8k45 gold badges154 silver badges261 bronze badges
1 Answer
Reset to default 2If you returned your file data as an octet stream, you could initiate the download like done here -> Save file Javascript with file name
uriContent = "data:application/octet-stream," + encodeURIComponent(dataFromServer);
newWindow=window.open(uriContent, 'filename.txt');
I'm sending a XMLHTTPRequest to a server endpoint using Polymer's iron-ajax element:
<iron-ajax
id="ajax"
method="POST"
url="/export/"
params=''
handle-as="json"
on-response="handleResponse"
</iron-ajax>
My Koa/Express-server responds with a read stream like this:
router.post('/export' , function*(){
var file = __dirname + '/test.zip';
var filename = path.basename(file);
var mimetype = mime.lookup(file);
this.set('Content-disposition', 'attachment; filename=' + filename);
this.set('Content-type', mimetype);
this.body = fs.createReadStream(file);
})
How do I initiate the download in handleResponse()
?
Ideally I don't want to handle the response at all and directly initiate the download.
The response headers look (as expected) like this:
Content-disposition: attachment; filename=test.zip
Connection: keep-alive
Transfer-Encoding: chunked
Content-type: application/zip
I'm sending a XMLHTTPRequest to a server endpoint using Polymer's iron-ajax element:
<iron-ajax
id="ajax"
method="POST"
url="/export/"
params=''
handle-as="json"
on-response="handleResponse"
</iron-ajax>
My Koa/Express-server responds with a read stream like this:
router.post('/export' , function*(){
var file = __dirname + '/test.zip';
var filename = path.basename(file);
var mimetype = mime.lookup(file);
this.set('Content-disposition', 'attachment; filename=' + filename);
this.set('Content-type', mimetype);
this.body = fs.createReadStream(file);
})
How do I initiate the download in handleResponse()
?
Ideally I don't want to handle the response at all and directly initiate the download.
The response headers look (as expected) like this:
Content-disposition: attachment; filename=test.zip
Connection: keep-alive
Transfer-Encoding: chunked
Content-type: application/zip
Share
Improve this question
edited Nov 4, 2015 at 17:37
Hedge
asked Nov 4, 2015 at 17:15
HedgeHedge
16.8k45 gold badges154 silver badges261 bronze badges
1 Answer
Reset to default 2If you returned your file data as an octet stream, you could initiate the download like done here -> Save file Javascript with file name
uriContent = "data:application/octet-stream," + encodeURIComponent(dataFromServer);
newWindow=window.open(uriContent, 'filename.txt');
本文标签: javascriptAjax response is a file downloadStack Overflow
版权声明:本文标题:javascript - Ajax response is a file download - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745599779a2158387.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论