Denzil Kruse wrote:
> Hi,
> 
> I want my users to download a csv file.  When they
> left click on the link, it brings the file up within
> the browswer.  They can of course do a right click and
> "Save Target As" depending on the browser.
> 
> But I don't want to fuss with instructions and
> confused users.  I would just like the same thing to
> happen as a "Save Target As" on a left click.

Very difficult to "force" the browser to do anything. The best you can do is
"suggest".

The most foolproof way is to use Content-Type: application/octet-stream (cf
RFC 2616, sec. 7.2.1). But that seems distasteful to me. Why lie about the
content type just because users are morons? Anyway...

Content-disposition is not part of the HTTP standard, so you'll get
inconsistent results.

Also, IE can think it's smarter than you are and inspect the URL for file
extensions instead of respecting your Content-Type header. Ain't life fun?

> 
> So I made something up, but it isn't working.  I get
> an internal server error from the browser.  When I run
> it at a command prompt, it seems to work fine.
> 
> Here is the code:
> 
> print "content-type: multipart/mixed\n";
> print "content-disposition: attachment;
> filename=SampleListingUpload.csv\n";
> 
> open (FILE,
> "<$ABS_PATH/httpdocs/SampleListingUpload.csv") or
>                 die "Unable to load the sample listing
> template. Reason: $!";
> 
> my $text = join "", <FILE>;
> 
> close FILE;
> 
> print $text;
> 
> And here is the output from a prompt:
> 
> -bash-2.05b$ ./sample_listing.cgi
> content-type: multipart/mixed
> content-disposition: attachment;
> filename=SampleListingUpload.csv
> Keywords,Bid Amount,Title,Url,Description
> home mortgage,0.10,ABC Home
> Mortage,http://www.ABCHomeMortgage.com,Instant Quotes
> tax attorney,0.75,ACME Tax
> Attorneys,http://www.acmetaxattorneys.com,Let us
> represent you
> -bash-2.05b$

multipart/mixed is incorrect. text/csv is the proper MIME type for your
response. Also, your MIME header lines techincally need to end with \r\n,
and you must have emit blank line after the header.

This is why we always recommend using the CGI module. It gets this stuff
right.

   use CGI qw(:standard);

   open FILE, ...blah blah...
   print header('application/octet-stream');
   print while <FILE>;

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to