Loading ...
Sorry, an error occurred while loading the content.

Re: [PBML] Downloading excel file using perl CGI script

Expand Messages
  • david wright
    I cannot reproduce your issue. The only thing I can add is make sure you are not printing *anything* before the print
    Message 1 of 4 , Jan 17, 2008
    • 0 Attachment
      I cannot reproduce your issue.

      The only thing I can add is make sure you are not printing *anything* before the

      print "Content-Type:application/vnd.ms-excel\n";

      I tried to reproduce your issue like such:

      #!/usr/local/bin/perl -Tw
      use strict;
      use CGI ();
      use CGI::Carp qw(fatalsToBrowser set_message); # for debugging info
      my $q = CGI->new();
      my $file = $q->Vars->{'file'};
      my $files_location = '/home/dwright/public_html/';
      open(FILE, "<$files_location$file") || die "Unable to download file
      $files_location$file\n";
      binmode FILE;
      my @fileholder = <FILE>;
      close (FILE);

      print "Content-Type:application/vnd.ms-excel\n";
      print "Content-Disposition:attachment;filename=$files_location\n\n";
      print @fileholder;


      and executed the script as such:
      http://localhost/xlstest.cgi?file=test.csv

      recieved output:
      browser presents a prompt to download said xml file


      +dave


      --- On Wed, 1/16/08, kshkid <madhanagopalm@...> wrote:

      > From: kshkid <madhanagopalm@...>
      > Subject: Re: [PBML] Downloading excel file using perl CGI script
      > To: "david wright" <david_v_wright@...>
      > Cc: perl-beginner@yahoogroups.com
      > Date: Wednesday, January 16, 2008, 12:36 AM
      > Thanks for the reply
      >
      > Am using apache 2.2.6
      >
      > and xls is a recognized mime type
      >
      > here is the entry in the file mime.types
      > *application/vnd.ms-excel xls xlm
      > xla xlc xlt xlw
      >
      > *am using both strict and warnings;
      >
      > For ease of reading I had just pasted the snippet.
      >
      > ### Interesting point is ###
      >
      > If am using a string literal like
      > my $file = "/home/work/file.xls";
      >
      > It works
      >
      > But when I use a variable ( for a dynamically generated
      > filename )
      > its not working and displaying the contents in the html
      > page itself.
      >
      > Could you please provide some pointers ?
      >
      > Thanks again
      >
      > On Jan 15, 2008 10:51 PM, david wright
      > <david_v_wright@...> wrote:
      >
      > > FWIW, your code works for me.
      > >
      > > I added,
      > > my $files_location =
      > '/home/dwright/public_html/test.csv';
      > >
      > > is xls a recognized mime type of your web server?
      > >
      > > i.e. on apache (ancient 1.3.26)
      > > /etc/apache/mime.types:
      > > application/vnd.ms-excel xls xlb
      > >
      > >
      > > In general if you are using perl to do cgi get in the
      > habit of enabling
      > > taint mode and warnings, yes, it will be harder to get
      > your script working
      > > but is worth it, you should also be using strict.
      > >
      > > #!/usr/local/bin/perl -Tw
      > > use strict;
      > >
      > > http://www.w3.org/Security/faq/
      > > http://gunther.web66.com/FAQS/taintmode.html
      > >
      > >
      > >
      > >
      > > --- On Tue, 1/15/08, kshkid
      > <madhanagopalm@...> wrote:
      > >
      > > > From: kshkid <madhanagopalm@...>
      > > > Subject: [PBML] Downloading excel file using perl
      > CGI script
      > > > To: perl-beginner@yahoogroups.com
      > > > Date: Tuesday, January 15, 2008, 2:13 AM
      > > > Hi All,
      > > >
      > > > Am working on a perl CGI script which
      > > > uploads excel sheet
      > > > processes it
      > > > downloads the processed excel sheet
      > > >
      > > > First two part of uploading and processing is
      > completed
      > > >
      > > > I have difficulty in getting the third part done
      > and could
      > > > not find any
      > > > potential answers after searching for it
      > > >
      > > > To download the file, I tried with the following
      > snippet
      > > >
      > > > ################
      > > > open(FILE, "<$files_location") ||
      > die
      > > > "Unable to download file
      > > > $files_location\n";
      > > > binmode FILE;
      > > > @fileholder = <FILE>;
      > > > close (FILE);
      > > >
      > > > print
      > > >
      > "Content-Type:application/vnd.ms-excel\n";
      > > > print
      > > >
      > "Content-Disposition:attachment;filename=$files_location\n\n";
      > > > print @fileholder;
      > > >
      > > > ################
      > > >
      > > > But with the above snippet, its displaying the
      > processed
      > > > excel file in (
      > > > binary format ) the html page itself
      > > >
      > > > Could you please help me by providing some
      > pointers on this
      > > > ?
      > > >
      > > >
      > > > Thanks
      > > >
      > > >
      > > > [Non-text portions of this message have been
      > removed]
      > > >
      > > >
      > > >
      > > > Unsubscribing info is here:
      > > >
      > http://help.yahoo.com/help/us/groups/groups-32.html
      > > > Yahoo! Groups Links
      > > >
      > > >
      > > >
      >
      >
      > [Non-text portions of this message have been removed]
      >
      >
      >
      > Unsubscribing info is here:
      > http://help.yahoo.com/help/us/groups/groups-32.html
      > Yahoo! Groups Links
      >
      >
      >
    Your message has been successfully submitted and would be delivered to recipients shortly.