Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

json - Ajax Response Content-Disposition attachment

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...