scoobyrico has asked for the wisdom of the Perl Monks concerning the following question:

Esteemed monks, I have some code that will allow the user to download a file off of the server. The code works well with Firefox, but not on Safari. With Safari, does seem to give me the file to view, just not to download. In Firefox, the download dialog pops up and I am able to save the file locally. The URL looks like this: http://www.somedomain.com/push.cgi?format=csv&file=filename.csv Thank you for your help in advance.
#!/usr/bin/perl ########################################################## use CGI qw(cookie header param); # parse form inputs my $file=param('file'); my $format=param('format'); if ($format eq 'Excel') { $type='binary'; $format='application/vnd.ms-excel'; } elsif ($format eq 'Comma') { $type='text'; $format='text/csv'; } else { $type='text'; $format='text/tsv'; } my $file = "/download/".$file; my $file2=$file; $file2=~s#download/##; print header( -type => qq($format; name="$file2"), -Content_disposition => qq(inline; filename="$file2") ); my $members=readfile($file,$type); print $members; exit; #======================================================== sub readfile { local($filename,$type)=@_; open(FILE,"<$filename"); if ($type eq 'binary') { binmode FILE; } local $/; $text=<FILE>; close(FILE); return $text; }

Replies are listed 'Best First'.
Re: Safari downloading files
by Joost (Canon) on Aug 16, 2007 at 16:03 UTC
    Your header is wrong, it should look something like
    print header( -type => $format, # no filename here -content_disposition => "attachment; filename=$file2");

    From RFC 2183 (emphasis mine):

    2.1 The Inline Disposition Type

    A bodypart should be marked `inline' if it is intended to be displayed automatically upon display of the message. Inline bodyparts should be presented in the order in which they occur, subject to the normal semantics of multipart messages.

    2.2 The Attachment Disposition Type

    Bodyparts can be designated `attachment' to indicate that they are separate from the main body of the mail message, and that their display should not be automatic, but contingent upon some further action of the user. The MUA might instead present the user of a bitmap terminal with an iconic representation of the attachments, or, on character terminals, with a list of attachments from which the user could select for viewing or storage.

      That was the trick!! Thank you for taking the time to get me the documentation also!