Ajax php return file for download
Find centralized, trusted content and collaborate around the technologies you use most. Connect and share knowledge within a single location that is structured and easy to search.
When I call above code directly from the browser, the result file is downloaded. But if I make an ajax call to above code, I don't get the download prompt.
I can see from console tab that the ajax call was successfully completed and a bunch of random characters is seen in the response data. I'm assuming that is the excel object. Does anyone know how I can achieve the download excel feature using ajax?
I don't want to refresh the page. When the user clicks on the "export" button, there should be an ajax call to the php file and prompt the user to download. You cannot download a file using ajax neither by phpexcel nor by php itself as its a security reason and almost browsers doesn't support it. But, you can try window. Also freakish answered for this type of question.
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 6 years, 11 months ago. Active 1 year, 5 months ago.
The demo page includes many other, 'better UX' examples as well. Noone posted this Pekka's solution It can help someone. The function base64ToBlob was taken from here and must be used in compliance with this function. This is good if your server is dumping filedata to be saved. In the above cases, you need to write the Content-Disposition header to the response , specifying that the file needs to be downloaded attachment and not opened by the browser inline.
You need to specify the Content Type too, and you may want to add the file name and length to help the browser drawing a realistic progressbar. With Struts2 unless you are using the Action as a Servlet, an hack for direct streaming , for example , you don't need to directly write anything to the response; simply using the Stream result type and configuring it in struts.
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. 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. Here is what I did, pure javascript and html.
Did not test it but this should work in all browsers. I try to download a CSV file and then do something after download has finished. So I need to implement an appropriate callback function. Using window. Something like this, change header so it is not a good idea. And window. You can refer this. This is my code, it is similar to the code of Shahrukh Alam.
But you should take care that window. When response has arrived, data will be stored into memory of browser. So before you click a link, the file has been downloaded. It means that you can do anything after download. Use window. And that way, the browser should prompt the user to save the file to disk, instead of just showing them the file. It will also automatically close the tab that it just opened. My approach is completly based on jQuery.
And I wanted it to be done by jQuery alone. What I and many others do is to create a link on the webpage, indicating that the target should be downloaded and putting the result of the http-request as the target. After that I append the link to the document than simply clicking the link and removing the link afterwards.
You don't need an iframe anymore. The interesting point is that this solution is only working with a " blob ". As you can see in other answers, some are simply using a blob but not explaining why and how to create it. As you can read e. The problem is that your http-response might not be any of those. Therefore the first thing you must do is to convert your response to a blob. This is what the first line does. Then you can use the " createObjectURL " with your newly created blob. If you than click the link your browser will open a file-save dialog and you can save your data.
Obviously it s possible that you cannot define a fixed filename for your file to download. Then you must make your response more complex like in the answer from Luke. And don't forget to free up the memory especially when you are working with large files. For more examples and information you can look at the details of the JS blob object. 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.
I found this helpful if the link runs a server side script that takes a little bit to compose the file before sending it. I think there's a way to read get data using js so then no php would be needed. If the server is writing the file back in the response including cookies if you use them to determine whether the file download started , Simply create a form with the values and submit it:. You need to reset the response or it will not download.
This is helpful in case when you want to decide whether or not file needs to be downloaded after making form. But I am referring to a page that must first be processed and then downloaded. I struggled with this issue for a long time.
Finally an elegant external library suggested here helped me out. 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.
Download a file by jQuery. Ajax Ask Question. 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. Keep in mind that this implementation uses plain JavaScript to make it easier for everybody to follow the example , but the actual download works the same for most frameworks jQuery, Vue, Angular, And, of course, you can find the entire implementation for this on GitHub.
0コメント