I'm trying to accomplish a fairly simple task for my website, but I'm not sure exactly how to go about it. I want the user to be viewing a table, then click a button, at which point the user can save the contents of that table as a csv file. This request can sometimes be quite complicated so I generate a progress page to alert the user.
I have most things figured out except actually generating the csv file. (I use jQuery and PHP)
the jQuery code run on click:
the relevant PHP:
What this does is sends the text as the PHP file, but it's doesn't generate a download. What am I doing wrong?
The_DenominaterThe_Denominater49544 gold badges77 silver badges1919 bronze badges
6 Answers
If you are forcing a download, you can redirect the current page to the download link. Since the link will generate a download dialog, the current page (and its state) will be kept in place.
Basic approach:
More complicated:
At PHP script:
The URL for both requests must be the same to trick the browser not to start a new download at
Ast DerekAst Derekdocument.location.href
, but to save the copy at the cache. I'm not totally sure about it, but seems pretty promising.2,61111 gold badge1515 silver badges2727 bronze badges
EDIT I just tried this with a 10MB file and it seems that val() is too slow to insert the data. Hurrumph.
Okay, so I gave this one another go. This may or may not be completely insane! The idea is to make an AJAX request to create the data, then use the callback to insert the data into a hidden form on the current page which has an action of a third 'download' page; after the insertion, the form is automatically submitted, the download page sends headers and echoes the POST, and et voila, download.
All the while, on the original page you've got an indication that the file is being prepared, and when it finishes the indicator is updated.
NOTE: this test code isn't tested extensively, and has no real security checks (or any at all) put in place. I tested it with a 1.5MB CSV file I had laying about and it was reasonably snappy.
Index.html
test.js
create.php
download.php
godheadatomicgodheadatomic
The best way to accomplish this is to use a Data URI as follows:
- Make the AJAX call to the server as per normal
- Generate the CSV on the server-side
- Return the data (either bare or inside a JSON structure)
- Create a Data URI in Javascript using the returned data
- Set window.location.href to the Data URI
See this link for instructions (paragraph #3, specifically): http://en.wikipedia.org/wiki/Data_URI_scheme
This way, you don't need to save any files on the server, and you also don't need to use iframes or hidden form elements or any such hacks.
AquarelleAquarelle5,98411 gold badge1111 silver badges1010 bronze badges
I don't think you can make the browser download using a AJAX/JS request. Try using a hidden iframe that navigates to the page which generates the CSV
Bob FincheimerBob Fincheimer15.1k11 gold badge2222 silver badges5252 bronze badges
Well the point of using AJAX is to avoid a visible reload of the page. If you want a download, you want the opposite,- a brand new request from the browser. I'd say, just create a simple button pointing to your php page.
Nicolas78Nicolas784,81711 gold badge1919 silver badges3636 bronze badges
To echo and expand on what others have said, you can't really send the file using AJAX. One of the reasons for this is (and someone correct me if I'm wrong on this, please) that the page you're currently on already has sent its content headers; you can't send them again to the same window, even with an AJAX request (which is what your PHP file is attempting to do).
What I've done before in projects is to simply provide a link (with target='_blank' or javascript redirect) to a separate download PHP page. If you're using Apache, check out mod_xsendfile as well.
godheadatomicgodheadatomic
Not the answer you're looking for? Browse other questions tagged phpajaxcsv or ask your own question.
I am using Python 3.3 on Windows. I am trying to figure out how to download a .csv file from yahoo finance. It is a file for the Historical Prices.
This is the source code where the link is I'm trying to access.
And here is the code I wrote to do it.
When I ran the code, I was expecting it to start the download and put it into my downloads folder, but it doesn't do anything. It runs and then stops. No csv file shows up in my downloads. So I think I'm missing something else in this code.
user2859603user2859603
11522 gold badges88 silver badges1616 bronze badges
2 Answers
You can do this with just urllib. The following code downloads the .csv file and puts the contents into a string named 'csv'. Then it saves the string to a file:
KevinKevin
Guy GavrielyGuy GavrielyCara Membuat File Csv
9,33044 gold badges2121 silver badges3737 bronze badges