Forum Moderators: coopster & phranque

Message Too Old, No Replies

How to create a csv file on the fly in Perl?

How to create a csv file on the fly in Perl

         

wjbarton

10:43 pm on Mar 2, 2004 (gmt 0)

10+ Year Member



Hello, how can I go about generating a csv file and sending it to a user without actually writing the file to the webserver's hard disk? I currently create a csv from some tables and store it on my webserver, then push it to the user with a self.location javascript tag. This sucks, because if someone were to get the URL of the csv they could just grab the last created copy.

How can I use perl to generate and send csv's on the fly? I've seen php examples, but can't get them ported properly to perl. I also want to avoid adding extra Perl modules. Any help is greatly appreciated.

Thanks!

- WJB

hyperbole

12:53 am on Mar 3, 2004 (gmt 0)

10+ Year Member



When you create the HTTP header send the MIME type as 'text/plain' and then send the csv as the content.

The user will see the csv in the browser and can save to their hard drive.

wjbarton

3:06 pm on Mar 3, 2004 (gmt 0)

10+ Year Member



Thanks for the response. Is this what you mean?

print "Content-type: text/plain\n";

Can't seem to get it to work...

- WJB

wjbarton

3:12 pm on Mar 3, 2004 (gmt 0)

10+ Year Member



Ah, I see you need two \n line breaks at the end of the statement. Ok, so now I have to look into how headers work, because I want to get this to work:

print "Content-type: application/vnd.ms-excel\n\n";
print "Content-disposition: attachment; filename=mydata.csv\n";
print "Pragma: no-cache\n";
print "Expires: 0\n";

Any ideas anyone?

Thanks,

- Warren

flashfan

4:26 pm on Mar 3, 2004 (gmt 0)

10+ Year Member



Check MIME::Lite module.

hyperbole

6:06 pm on Mar 3, 2004 (gmt 0)

10+ Year Member



It seems to me you can do that with the CGI module.

wjbarton

11:12 pm on Mar 3, 2004 (gmt 0)

10+ Year Member



I got it to work with this code and no modules at all:

print "Content-type: application/msexcel\n";
print "Content-disposition: inline; filename=\"test.csv\"\n";
print "Pragma: no-cache\n";
print "Expires: 0\n\n";

The simple key was to not double-break (\n\n) until I finished printing the headers.

Thanks all,

- WJB

hyperbole

9:48 pm on Mar 4, 2004 (gmt 0)

10+ Year Member



Yes the HTTP standard states that "a blank line indicates the end of the header". This means putting two new-lines in a row to create a blank line.

You don't want to put a blank line prior to the end of the header because the HTTP protocol will think that is the end of the header.