Chrome ajax download file






















Depending on your needs, you could use it instead of an AJAX request or using window. You can't have an AJAX request open the download prompt since you physically have to navigate to the file to prompt for download.

Instead, you could use a success function to navigate to download. This will open the download prompt but won't change the current page. Even though this answers the question, it's better to just use window. You actually don't need ajax at all for this. If you just set "download. The browser should recognise the binary download and not load the actual page but just serve the file as a download.

It is possible. You can have the download started from inside an ajax function, for example, just after the. I have an ajax function that exports a database of contacts to a. So, after I get the responseText and everything is Ok, I redirect browser like this:. For those looking a more modern approach, you can use the fetch API. The following example shows how to download a spreadsheet file.

It is easily done with the following code. Also, it has a similar syntax to the jQuery approach, without the need to add any additional libraries. Of course, I would advise checking to which browser you are developing, since this new approach won't work on IE.

You can find the full browser compatibility list on the following link. This url must be set, on my example I am assuming you know this part. Also, consider the headers needed for your request to work. Joao Marcos solution works for me but I had to modify the code to make it work on IE, below if what the code looks like.

This solution is not very different from those above, but for me it works very well and i think it's clean. Email Required, but never shown. The Overflow Blog. Podcast what if you could invest in your favorite developer? Who owns this outage? Building intelligent escalation chains for modern SRE.

Featured on Meta. Now live: A fully responsive profile. Reducing the weight of our footer. Linked Related Hot Network Questions. Response might be a JSON string or it might be a file as an attachment. I can easily detect Content-Type and Content-Disposition in my ajax call, but once I detect that the response contains a file, how do I offer the client to download it?

I've read a number of similar threads here but none of them provide the answer I'm looking for. Please, please, please do not post answers suggesting that I shouldn't use ajax for this or that I should redirect the browser, because none of this is an option.

Using a plain HTML form is also not an option. What I do need is to show a download dialog to the client. Can this be done and how? Don't give up so quickly, because this can be done in modern browsers using parts of the FileAPI:.

Create a form, use the POST method, submit the form - there's no need for an iframe. When the server page responds to the request, write a response header for the mime type of the file, and it will present a download dialog - I've done this a number of times. That excel file is created by the server and returned as a response to the client. Download that response as a file with custom name in browser ". Here we need to carefully set few things at the server side.

I set few headers in Python Django HttpResponse. You need to set them accordingly if you use other programming languages. Since I download xls excel here, I adjusted contentType to above one.

You need to set it according to your file type. You can use this technique to download any kind of files. What server-side language are you using?

This will in fact 'redirect' the browser to this download page, but as ahren alread said in his comment, it won't navigate away from the current page. It's all about setting the correct headers so I'm sure you'll find a suitable solution for the server-side language you're using if it's not PHP.

The server then generates a link from where this file can be downloaded, e. For example, the server responds with:.

When processing the response, you inject an iframe in your body and set the iframe 's SRC to the URL you just received like this using jQuery for the ease of this example :. If you've set the correct headers as shown above, the iframe will force a download dialog without navigating the browser away from the current page.

After you've received the JSON response, you can then decide client-side what to do with it. Maybe, for example, later on you want the user to click a download link to the URL instead of forcing the download directly, in your current setup you would have to update both client and server-side to do so. I see you've already found out a solution, however I just wanted to add some information which may help someone trying to achieve the same thing with big POST requests. I had the same issue a couple of weeks ago, indeed it isn't possible to achieve a "clean" download through AJAX, the Filament Group created a jQuery plugin which works exactly how you've already found out, it is called jQuery File Download however there is a downside to this technique.

In slow Internet connections you'll have to wait a lot until the request is sent and also wait for the file to download. But as I discovered in my own app, for bigger file sizes it is almost unbearable. My app allow users to export images dynamically generated, these images are sent through POST requests in base64 format to the server it is the only possible way , then processed and sent back to users in form of.

In slow Internet connections it can be really annoying. Updated on January 3, Published on February 6, Demo Download Starting Download.. In this Tutorial. If both filename and saveAs are specified, the Save As dialog is displayed, populated with the filename. This is an asynchronous function that returns a Promise.

An object specifying what file you wish to download, and any other preferences you wish to set concerning the download. It can contain the following properties:. A boolean flag that enables downloads to continue even if they encounter HTTP errors. Using this flag, for example, enables the download of server error pages. Default value false.



0コメント

  • 1000 / 1000