Guide ajax json file downloaded






















I looked your edited solution but I think that it can sacrifice the performance because it has to make a double request. So if anyone needs another solution that doesn't imply to call the service twice then this is the way I did it:. This form is just used to call the service and avoid to use a window. After that you just simply have to make a form submit from jquery in order to call the service and get the file. It's pretty simple but this way you can make a download using a POST.

I now that this could be easier if the service you're calling is a GET , but that's not my case. I used this FileSaver. In my case with csv files, i did this in coffescript :. I think for most complicated case, the data must be processed properly. Under the hood FileSaver. To get Jonathan Amends answer to work in Edge I made the following changes:. Below is my solution for downloading multiple files depending on some list which consists of some ids and looking up in database, files will be determined and ready for download - if those exist.

And Yes, like others said, it is possible to do it in jQuery Ajax. I did it with Ajax success and I am always sending response As long as you return response , success in Ajax can work with it, you can check if file actually exist or not as the line below in this case would be false and you can inform user about that:.

But I am referring to a page that must first be processed and then downloaded. I know browsers block multiple file downloads, and I also have API which returns a set of csv formatted data. If anyone can help me improve this that would be great, but it's working for me so far. 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. Handle file download from ajax post Ask Question. Asked 8 years, 7 months ago. Active 1 year, 1 month ago.

Viewed k times. Pavle Predic Pavle Predic 5, 3 3 gold badges 15 15 silver badges 16 16 bronze badges. For those who read this article, read this post: stackoverflow. Add a comment. Active Oldest Votes. URL window. Jonathan Amend Jonathan Amend I don't want to navigate away from the page. I want to perform the request in the background, process the response and present it to the client.

If the server sends back headers like the other answer has, it opens in a new window - I've done it before. It would only navigate away if your server-side script returned HTML code — user PavlePredic did you end up figuring out how to manage both response scenarios, i.

JSON text response or download file response? Answer is not clear and the proposed solution doesn't work. Show 5 more comments. I faced the same issue and successfully solved it. 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.

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, 10 months ago. Active 7 months ago. Viewed k times. Thank you in advance. 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. Community Bot 1 1 1 silver badge. Steven Lambert Steven Lambert 5, 1 1 gold badge 25 25 silver badges 44 44 bronze badges.

Doesn't this call the link twice? Think about it this way: your website is busy at work responding to input, scrolling around and interacting with the user. So your website gets its friend AJAX to go get that file for it. Your website keeps doing its thing, and AJAX drives across town to get the file. Eventually AJAX comes back with the file, and your website can do whatever it wants with it.

Normally, the function is called 4 times:. The readyState variable will be an integer from 1 to 4 for each of the above states, or 0 if something went wrong. Sorry I got lost on my way to the file! Inside the onreadystatechanged function, you can check the readyState variable:.

For our purposes, we probably only care about readyState being 4 , but you can use the other states for debugging or for showing progress. The readyState variable only tells us half the story.

So we also have to check this status, which is stored in the status surprise! The status variable holds the HTTP status code of the request. Here is a full list of possible values, but for now we probably only care about a status of success. It checks for the readyState to be 4 , meaning that the request has been completed. Then it checks for the status to be , meaning that the file was loaded successfully.

Finally, we can do that using the responseText variable. The responseText variable holds the content of the file. For example, we could just set the content of one of the elements in our webpage to whatever is in the file:. This line of code finds the element with an id of welcome and sets its content to responseText , which contains the content of the file. But you can make an AJAX request whenever you want, not just when the page loads.

Now instead of fetching the message as soon as the page loads, the code waits until the user clicks to fetch it. I don't see it in the docs. DarinDimitrov How to show these data in a carousel bootstrap? Rafay Rafay Note array notation json[0] — Rafay. Thank you. How to get specified column value in a db table? PHPFan you mean how to query database table?

Sarabhaiah Polakam Sarabhaiah Polakam 3 3 silver badges 2 2 bronze badges. Rana Zohaib 3 3 3 bronze badges. Mohammed Abdelrasoul Mohammed Abdelrasoul 2 2 silver badges 12 12 bronze badges. Use that code.

Shivam Srivastava Shivam Srivastava 4, 2 2 gold badges 20 20 silver badges 24 24 bronze badges. I agree with all the above solutions, but to point out whats the root cause of this issue is, that major role player in all above code is this line of code: dataType:'json' when you miss this line which is optional , the data returned from server is treated as full length string which is default return type.

Any jQuery ajax calls should specify this line, if expecting json data object. Servesh Mishra Servesh Mishra 31 2 2 bronze badges.



0コメント

  • 1000 / 1000