admin管理员组文章数量:1332108
How does one go about setting up file download in a Single Page Application, without triggering a reload?
I have had a situation where a PDF file is generated on the server and needs to be given to the client for download. Sending it as an application/octet-stream does nothing useful in a SPA, because files can't be sent over AJAX.
The best I came up with is saving the generated file in a temp folder on the server, sending file's URL to the client and doing window.open(url)
. The problem is that different browsers open files differently. Firefox for instance, by default, opens PDFs in the same tab, using their PDF.js
, disrupting the whole SPA idea. But doing a window.open(url, '_blank')
often triggers popup blockers etc. Other file types can cause God knows what...
Is there a cross-browser, safe, general method for downloading files in a SPA?
How does one go about setting up file download in a Single Page Application, without triggering a reload?
I have had a situation where a PDF file is generated on the server and needs to be given to the client for download. Sending it as an application/octet-stream does nothing useful in a SPA, because files can't be sent over AJAX.
The best I came up with is saving the generated file in a temp folder on the server, sending file's URL to the client and doing window.open(url)
. The problem is that different browsers open files differently. Firefox for instance, by default, opens PDFs in the same tab, using their PDF.js
, disrupting the whole SPA idea. But doing a window.open(url, '_blank')
often triggers popup blockers etc. Other file types can cause God knows what...
Is there a cross-browser, safe, general method for downloading files in a SPA?
Share Improve this question edited May 23, 2017 at 11:46 CommunityBot 11 silver badge asked Jan 24, 2014 at 11:47 SljuxSljux 53410 silver badges28 bronze badges 1- does a window.open(url, '_new') have the same affect – Warrenn enslin Commented Jan 24, 2014 at 11:49
1 Answer
Reset to default 5In SPA application I wrote a while back the the window.location.href = '...'
did the trick and worked correctly for most browsers, but the contentType and content-disposition header of the page being downloaded is what makes the biggest difference. if the browser can recognize the file as a type do be downloaded then chances are the SPA won't break on redirect.
Also just a quick note SPA frameworks like Angular sometimes allow for target='_new'
and target='_self'
on their a tags without interfering with the routes and such.
本文标签: javascriptDownloading files in a SPAStack Overflow
版权声明:本文标题:javascript - Downloading files in a SPA - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742257102a2441826.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论