Ajax Response Content-Disposition attachment

喜欢而已 提交于 2019-11-27 22:44:39

问题


I am building a website that communicates via javascript with a webservice that accepts posted json requests. I Post these requests using XMLHttpRequest. In some cases the service returns a download response:

Example:

Content-Disposition: attachment; filename="somefile.txt"
Content-Type: application/force-download
Content-Length: 3

123

if document.location is pointed to an URL that serves a response like the one above, the browser will automatically prompt the user with a file download dialog.

My question is: Can I achieve the same effect with XMLHttpResponse objects somehow? is there an Iframe trick or something? Unfortunately there is no target member on XMLHttpRequest objects like there is on html forms, cause if I could just redirect the full response (with headers) to a new page or hidden iframe the problem would be solved, I guess.


回答1:


I guess not. But you can try submitting hidden form to a hidden iframe.



来源:https://stackoverflow.com/questions/7464665/ajax-response-content-disposition-attachment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!