Js download file from ajax






















In case you don't use FPDF and need to set it manually simply add this before the output:. The more interesting thing about this is how the file is downloaded after sending the HTTP request. Let's dive straight into it:. The actual download is done by creating a Blob object, which is used for a newly created a tag with a link to the created Blob object which is automatically clicked which ultimately opens the "Save file" dialog. Additionally it's appended to the body which is a fix for Firefox and is removed from the body afterwards we don't want to have tons of invisible a tags on our body.

If the value is not provided, the original filename is used. As you can see above, the download attribute is the easiest way to download a file on the browser. However, the download attribute only works in modern browsers. Internet Explorer does not support it. The idea is to programmatically create an anchor link, and then trigger the click event. This method is especially helpful for dynamically generated file URLs.

Sometimes, you may want to save programmatically generated data as a file using JavaScript. That's where blobs and object URLs are useful. A Blob object is a file-like object used to represent raw immutable data. 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. I prefer location. Telmo Dias Telmo Dias 3, 2 2 gold badges 31 31 silver badges 42 42 bronze badges.

Alain Cruz Alain Cruz 3, 3 3 gold badges 19 19 silver badges 37 37 bronze badges. Jemil Oyebisi Jemil Oyebisi 7 7 silver badges 8 8 bronze badges. Decoding a filename from the header is a little bit more complex Jaime 4, 2 2 gold badges 19 19 silver badges 42 42 bronze badges.



0コメント

  • 1000 / 1000