Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

How to download a file from the server

by ghosh123 (Monk)
on Jul 23, 2013 at 11:41 UTC ( [id://1045840]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monk
I have a cgi script which has a "a href" tag link on it and below that link a table containing some data. The link is for creating an excel report which will contain the table data on that page.
Now in the background , clicking on that link , it redirects to another script (createReport.pl)and generates the excel report. Now I want to download that report also , but that is not working.

My cgi script is below :

use CGI; my $cgi = CGI->new (); my $js = <<'JS'; function reportCreate() { var currenturl = document.URL; var url = "https://autoreport.com:8001/cgi-bins/createReport.pl?url="+ +currenturl; window.location.href = url ; return 1; } JS print qq{<script type = "text/javascript">$js</script>}; print $cgi->a({-href=>"javascript:void(0);", -onclick => "reportCreate +();" }, 'Create Report <br>'); print qq{<TABLE BORDER=1><TR BGCOLOR="#D0D0D0"><TH ALIGN=LEFT NOWRAP +>Name}; print qq{</TH>}; ## some Table html code here...please assume print qq{</TABLE>};

Now the above code is working fine. Below I am givnig the createReport.pl script to which the above script redirects. createReoprt.pl is creating the excel sheet report and pushing the content of the table inside it successfully. But I am concerened about the code below that for downloading which is not working.

use Spreadsheet::WriteExcel; use HTML::TableExtract qw(tree); require LWP::UserAgent; my $filename = "/home/user/report.xls"; # The excel generation using Spreasheet # The table extraction as element using TableExtract # All fine till now , and the excel is getting generated # with the desired content. # But this below piece of code is not working, please let me know how + to make it work : open(FILE, "<$filename ") || die "Unable to download file \n"; my @fileholder; binmode FILE; @fileholder = <FILE>; close (FILE); print "Content-Type:application/vnd.ms-excel\n"; print "Content Disposition:attachment;filename=report.xls\n\n"; print @fileholder;

Please help.

Replies are listed 'Best First'.
Re: How to download a file from the server (content disposition CGI->header( qw{ -type mime/type -attachment filename } ) )
by Anonymous Monk on Jul 23, 2013 at 12:05 UTC

      Sorry , I did not get how to use it and it still not working. Can you please elaborate ?

        Sorry , I did not get how to use it and it still not working. Can you please elaborate ?

        The code you posted, the headers it generates, doesn't match the headers CGI::header generates

Re: How to download a file from the server
by Anonymous Monk on Jul 23, 2013 at 11:43 UTC

    which is not working.

    It explodes again?

      Upon clicking on that a href link , it simply displays the content on the createReport.pl page as :

      Content-Type:application/vnd.ms-excel Content-Disposition:attachment;f +ilename=report.xls


      Whereas I am expecting a download dialog to appear and letting me download the report.xls

        it simply displays the content

        you need to look the HTTP, at the bytes, try lwp-download or wget --debug and look at the bytes

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1045840]
Approved by Happy-the-monk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-25 17:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found