Ajax post request download file
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. Liked this post? Glad to hear that! Get the next one delivered right to your inbox. Check your inbox or spam folder and click the link to confirm your subscription.
The params are sent as proper post params as if coming from an input rather than as a json encoded string as per the previous example. There might be a safer way to encode those variables. Alternatively contemplate escaping them. 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 on 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.
Here is what I did, pure javascript and html. Did not test it but this should work in all browsers. You need to reset the response or it will not download. I found a fix that while it's not actually using ajax it does allow you to use a javascript call to request the download and then get a callback when the download actually starts.
How to avoid that? There is absolutely no need to use ajax for this at all. Just call window. All this is doing is making the same request twice. I have no idea why this was upvoted. Show 4 more comments. Equals Constants. Combine Path. Message ; return Content e. GetExtension path. Raw Localizer["Report has been exported successfully"]. ReadAllBytes file1. FullName ; return File fileBytes, System. Dilshad Dilshad 1 1 1 bronze badge. A big block of code thrown up on the web without explanation doesn't help anyone.
You should explain your approach and why your answer is better or different from the other answers.
0コメント