Ajax post download file






















What is the parameter on the server for posted file? Can you please post server part. Musa HogRider - if you Google your error message, this is the first result: stackoverflow.

As an aside, it is not best practice to simply copy-and-paste code blindly without first understanding it. I would recommend you go through the code line-by-line to gain an understanding of what is happening before putting the code to use. I am using local by way xampp to be exact. Can i ask a question that maybe you can clarify?

Brownman Revival : I know that its too late for the reply.. You got a cross origin error because you opened the html file as file than running it from the server. How do I apply form action based on selection in this code? Show 1 more comment. Giulio Caccin 2, 6 6 gold badges 31 31 silver badges 52 52 bronze badges.

When you merely want to refer to another page as an answer, you can vote to close as a dupkucate or leave a comment under the question. This post is Not An Answer. A post of this kind looks like an attempt to farm rep. This is the complete solution, the PHP bit helps too. Boghyon Hoffmann Gvice Gvice 2 2 silver badges 10 10 bronze badges.

Using this method, you can post form but not with 'file' type fields. This question is specifically about file upload. Thanks for posting your answer! POST, request. FILES if form. Jay Jay 1, 2 2 gold badges 8 8 silver badges 22 22 bronze badges. How dow this improve any of the answers already given? Also this answer mentions a views. Use FormData. It works really well All is well.

Use a hidden iframe and set your form's target to that iframe's name. This way, when the form is submitted, only the iframe will be refreshed. Have an event handler registered for the iframe's load event to parse the response.

Manki Manki 3, 4 4 gold badges 23 23 silver badges 18 18 bronze badges. View Demo. Related Articles. Add Comments. Thank you for the feedback. The comment is now awaiting moderation.

You will be notified via email when the author replies to your comment. 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 Stats Please Sign up or sign in to vote. Create and download the file with Ajax call in ASP. Copy Code. Member Jul Majid Qafouri Jan Ghouse M 1-Jan MarceloGuy Sep JoshBerke JoshBerke No support for download attribute on IE and Safari : caniuse. You can open a new window and put content there, but not sure about a PDF or Excel You should free up browser memory after calling click : window.

JoshBerke This is old but it works on my end. How can I get it to open up my directory immediately to save rather than saving to my browser? Also is there a way to determine the name of the incoming file? It will corrupt binary files, because they get converted to strings using encoding, and result is not very close to original binary May use mimetypes to not corrupt the information as used here with PDFs: alexhadik.

Data; formGenerate. Al though, there are few alternatives but they are not perfect and won't work on all browsers: Look at data URI scheme. If binary data is small then you can perhaps use javascript to open window passing data in URI.

VinayC VinayC It looks like that may be the only way to do it in a single request. But it isn't really the direction I want to go. I'd used URI data for thumbnails before, but it never occurred to me to use it for downloads - thanks for sharing this nugget. MapPath string. ReadAllText path ; System. Frank Frank 3, 1 1 gold badge 24 24 silver badges 36 36 bronze badges. I think I like this solution best. In my case though I am generating the file, so I think that I will try keeping the file in memory until it is accessed and then free up the memory after it's downloaded so I never have to write to disk.

I like this solution. In the OP's question it sounds like the requester knows whether to expect a file download or JSON data, so he could decide at the client end and post to a different URL, rather than deciding on the server end. My second approach is looking more appealing to me now too. Thanks for confirming it is a worthwhile solution. Are you suggesting that I pass an additional value in the json object that indicates this request is being made from a browser as an ajax call instead of a web service call?

There are several ways to accomplish this that I can think of, but what technique you would use? Or any other http header? Otis-iDev Otis-iDev 61 4 4 bronze badges. This will work, for small files. But if you have a large file you are going to run into the problem of the url being too long.



0コメント

  • 1000 / 1000