Download file via ajax request javascript






















As others have stated, you can create and submit a form to download via a POST request. However, you don't have to do this manually. One really simple library for doing exactly this is jquery. It provides an API similar to the standard jQuery. This is a 3 years old question but I had the same problem today. 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. My use-case is this. After fetching content as a blob binary , we are creating a downloadable URL and attaching it to invisible "a" link then clicking it.

Naren Yellavula Naren Yellavula 6, 2 2 gold badges 27 27 silver badges 22 22 bronze badges. Robin van Baalen Robin van Baalen 3, 2 2 gold badges 19 19 silver badges 33 33 bronze badges. Community Bot 1 1 1 silver badge. Mayur Padshala Mayur Padshala 1, 2 2 gold badges 18 18 silver badges 19 19 bronze badges. Hi, do I need to have jQuery 3. I'm also getting a blank pdf with both examples you gave. I am trying to get it to download a pdf file.

Active Oldest Votes. Govinda Rajbhar Govinda Rajbhar 2, 3 3 gold badges 35 35 silver badges 59 59 bronze badges. This example worked for me. This example sends an HttpGet request, and thus the data payload just one record ID in the request is included as parameters in the URL. But, what if I wanted to send an HttpPost request, with the data payload included as an object containing many fields in the request?

Gedao Gedao 1, 9 9 silver badges 13 13 bronze badges. Is it easier for you to pass model id to the action and then find your student by id and do whatever you want with student model data. I have updated the answer. There's no dual access to DownloadAttachment method? This will call DownloadAttachment method 2 times. 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"]. Collectives on Stack Overflow. Learn more. Asked 11 years, 10 months ago. Active 1 year, 7 months ago.

Viewed k times. I have an action class that generates a PDF. The contentType is set appropriately. Mike Nayn Nayn 3, 8 8 gold badges 36 36 silver badges 47 47 bronze badges. Add a comment. Active Oldest Votes. Community Bot 1 1 1 silver badge. Mayur Padshala Mayur Padshala 1, 2 2 gold badges 18 18 silver badges 19 19 bronze badges.

Does it work on chrome? I can only see a blank pdf. Yes, it does work on all modern browsers. If you see a blank pdf, try running the ajax url in a new tab.

If you get a blank screen there as well, might be a problem with the pdf itself. If you do see a pdf file there and not in the downloaded file, do let me know on my email. This anchor element actually did not work for me on IE 11, Edge and Firefox. Show 7 more comments. BalusC BalusC 1. Once again: you do not need Ajax for this. It's only asking for trouble. In some browsers the window. So, also do NOT use window.

If the content-disposition is set to attachment , you will just get a Save as dialogue. The parent page will stay unchanged. There is a limited Url length.

And the author is asking about POST. Show 10 more comments. URL window. It is reusable. Hope that helps. Georgios Syngouroglou Georgios Syngouroglou I had to set the responseType of xhr to arraybuffer or blob for this to work. Otherwise, this works great. I had the exact same question. All the people responding "just make it a link" doesn't help the OP. If your content is dynamic and the link you are going to is dynamic, you have to jquery it all Works great. This is a great answer, but for some reason, I just keep getting broken empty PDF.



0コメント

  • 1000 / 1000