Use ajax to download file






















You will be notified via email when the author replies to your comment. Please select a comment to reply. You can add your comment about this article using the form below. Make sure you provide a valid email address else you won't be notified when the author replies to your comment Please note that all comments are moderated and will be deleted if they are Not relavant to the article Spam Advertising campaigns or links to other sites Abusive content.

Please do not post code, scripts or snippets. Required Invalid Email Address. Security code:. Required Invalid security code. I declare, I accept the site's Privacy Policy. Add Comment. Disclaimer : The code samples and API available at www. You are free to use it for commercial as well as non-commercial use at your own risk, but you cannot use it for posting on blogs or other tutorial websites similar to www.

All the code samples and API provided by the authors are solely their creation and neither the author nor the site are responsible if it does not work as intended. I agree to the above terms. Download Required. What our readers say. Error Details. This solution is not very different from those above, but for me it works very well and i think it's clean. Your needs are covered by window. But I am referring to a page that must first be processed and then downloaded.

Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow.

Learn more. Asked 7 years, 11 months ago. Active 7 months ago. Viewed k times. Thank you in advance. Improve this question. This will not work, see [this question][1]. Do location. When you need this, it does feel like it's a common thing to ask for, and sadly with no elegant solutions.

Add a comment. Active Oldest Votes. Original answer You can't have an AJAX request open the download prompt since you physically have to navigate to the file to prompt for download. Improve this answer. Community Bot 1 1 1 silver badge. Steven Lambert Steven Lambert 5, 1 1 gold badge 26 26 silver badges 44 44 bronze badges. Doesn't this call the link twice? I'm in a similar boat I'm passing a lot of security information in headers, and able to parse the file object in the success function, but don't know how to trigger a download prompt.

It does call the page twice, so if you are querying a database in that page, this means 2 trips to DB. Let me explain how this helped me I have an ajax function that does some error checking on a form submission and then creates a csv file. If the error check fails, it has to come back with why it failed.

If it creates the CSV it is telling the parent that "go ahead and fetch the file". But it will send request 2 times, that is not proper — Dharmendrasinh Chudasama. Show 4 more comments. Taha I tested this on Edge and it seemed to work. Don't know about IE though. My client doesn't target IE users ;- Am I lucky? You certainly want to hide it at the same time. Jelle Kralt Jelle Kralt 6 6 silver badges 15 15 bronze badges.

The programming language you're using to change window. You're right mikemaccana, I actually meant ajax :. Have been hunting high and low for a solution and this is so elegant and perfect.

Thank you so much. Of course, this solution will only work if it is a static file that already exists. If the server responds with an error though there won't be any way to stay on your main page without being redirected to an error page by the browser.

At least this is what Chrome does when the result of window. Show 1 more comment. Good Generic Code. Thanks leo. Its helpful. Also what do you suggest adding window. The filename will be wrong if the content disposition specifies a non-UTF8 filename. So, after I get the responseText and everything is Ok, I redirect browser like this: window.

Pedro Sousa Pedro Sousa 6 6 silver badges 11 11 bronze badges. Isn't this dangerous security-wise? I would think so because anybody can call download. Since the access has reached a PHP script, htaccess now stops its duty. One should always sanitise the requested file to be read — Prof. Show 2 more comments.



0コメント

  • 1000 / 1000