当前位置: 动力学知识库 > 问答 > 编程问答 >

Jquery ajax function and downloading files

问题描述:

I am using $ajax( { type:"post", url:"/whatever/" } ) to access my server which works nicely. The problem is that the response is not evaluated as I need it.

On some occations the server sends back a dummy string ("x") which can be ignored on the browser, but on other occasions my server sends back a special file as attachment:

mimetype application/x-my-special-type

Content-Disposition attachment; filename=myname.ext

....

But the browser (I am only interested in Google Chrome / Chromium - it's an intranet thing) does not show the download popup window. (Calling the "/whatever/" URL directly in the address bar actually DOES show the download popup window to save the file.)

How can I persuade jQuery to show the usual download popup window?

网友答案:

Send the user to the URL by setting window.location.href or linking directly to the file. Do not use AJAX for this, as it is only used for retrieving data to variables/inserting to DOM. You'll probably need to think of a different way to represent the no-response case. Perhaps the AJAX portion could be used to establish if a file is available or not, and the redirection would occur if the AJAX response indicates that a file is available.

分享给朋友:
您可能感兴趣的文章:
随机阅读: