Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: csv file download size limits?

by Tux (Monsignor)
on Nov 08, 2013 at 07:35 UTC ( #1061672=note: print w/ replies, xml ) Need Help??


in reply to csv file download size limits?

Just a side note: you are generating both invalid and probably even unparseable CSV. In the header line, you are including spaces after the comma, causing the header fields to be seen as e.g.  Amount ("<space>Amount"). I cannot believe that to be intentional. Inside the loop you just spit out all fields joined on ,. Please try to think what will happen if any of the dat contains a comma. Having amounts makes that highly likely. The US uses $ 2,000,000.00 other countries might use 2.000.000,00. Try to think about your data consumer. Please use a CSV generator like Text::CSV_XS which makes your code not only correct, but also removes the need for extra undefined checks.

print $cgi->header ( -type => "text/csv", -charset => "utf-8", -attachment => "data.csv", ); my $csv = Text::CSV_XS->new ({ binary => 1, eol => "\r\n" }); $csv->print (*STDOUT, [ "Platform ID", "Processor ID", "Transaction ID", "Date", "Settlement Date", "Transaction type", "Status", "Amount", "First Name", "Last Name", "Address1", "Address(cont.)", "City", "State or Province", "Postal Code", "Country", "Phone", "Email", "Processor response code", "MSC response code", "IP Address", "CC Number", "Descriptor" ]); foreach my $v (@a) { my ($m_id, @r) = @$v; $r[6] = $r[6] ? "succeeded" : "failed"; $csv->print (*STDOUT, [ @r, $descriptors{$m_id}{$r[0]} ]); } $dbh->disconnect;

Enjoy, Have FUN! H.Merijn


Comment on Re: csv file download size limits?
Select or Download Code
Re^2: csv file download size limits?
by ted.byers (Scribe) on Nov 08, 2013 at 15:47 UTC

    Thanks for this.

    Point noted. In this particular case, the data is not formatted. Rather, it is a straight dump of data from the DB. But, I will have to take a look at that, to ensure that the CSV file MySQL produces is valid. The amount field won't be a problem as it is an unformatted decimal number, but one of the address fields may well contain commas. IIRC, MySQL allows one to add a clause that encloses all fields in single or double quotes, which would prevent problems if one field or another includes commas as a part fo the data.

    Thanks

    Ted

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1061672]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (13)
As of 2014-10-23 12:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (125 votes), past polls