Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PDF Download 1

Status
Not open for further replies.

JimJx

Technical User
Joined
Feb 16, 2001
Messages
202
Location
US
Hello all,

I have a page that lets people choose a PDF to download via radio buttons. Everything works fine on my end. However, client says that when they do the download they get an error and cannot open the file.

My guess is there is a problem on her box, but I cannot convince her that running win2k on a mac g4 using virtual PC is the problem. [ponder]

So, just to make sure I have all of my ducks in a row before I tell her to get over it, would you all be so kind as to take a quick look at the script and see if I missed anything?

I know that this isn't the pretties code around, but any suggestions appreciated.....

Jim

Code:
my $query = new CGI;
my $file = $query->param("file");
my $filename;
my $filesize;
my $DLfile;
my $first_name = $query->param("first_name");
my $last_name = $query->param("last_name");
my $company = $query->param("company");
my $address = $query->param("address");
my $city = $query->param("city");
my $state = $query->param("state");
my $zip = $query->param("zip");
my $phone = $query->param("phone");
my $fax = $query->param("fax");
my $email = $query->param("email");
my $Track = $query->param("Track");
my $filePath = '/pdfs/';

if ($file eq "RegDShort") {
$filename = "Agreement.pdf";
}
if ($file eq "RegDLong") {
$filename = "RegDOffering.pdf";
}

my $filesize = -s $filename;
# print full header 
print "Content-disposition: attachment; filename=$filename\n";
print "Content-Length: $filesize\n";
print "Content-Type: application/pdf\n\n";

# open in binmode 
open my $read,'<',$filePath.$filename or die "Failed to read $filename - $!";       
binmode $read;
     
# stream it out binmode 
binmode STDOUT;
while (<$read>) {
print;
}
 
What error?
Yours?
Code:
"Failed to read $filename - $!";
or her machine throwing its toys out of the pram?

Keith
 
Ask your client to try another computer if possible. The code looks OK as far as the download goes. I don't know if "binmode STDOUT" is actually doing anything though. Your code could maybe be a bit more secure but thats another issue.

- Kevin, perl coder unexceptional!
 
actually..... I tested a couple of things uisng Mozilla. I uploaded a pdf file in ASCII mode and binary mode. It worked either way for me once the file was downloaded successfully and opened by Acrobat so that didn't seem to matter.

What did matter for my test though was reading the filesize from the wrong directory. You have the file in a folder named "pdfs". I made a "pdfs" folder in the cgi-bin and then ran the code. Since the filesize is only being determined using the filename, the script returned an undefined value for the filesize:

my $filesize = -s $filename;

and Mozilla did not download the file and acrobat errored out when trying to open and read the empty file.

If your script also resides in the 'pdfs' folder this should not be a problem, but if it doesn't you need to chdir to the "pdfs" folder or use the absolute filepath to the file to get the correct size for the content-length header. Ignoring all your extra form field stuff, this works OK for me:

Code:
#!/usr/bin/perl
use CGI;
use CGI::Carp qw/fatalsToBrowser/;
use strict;
use warnings;

my $query = CGI->new;

my $filePath = '/home/********/public_html/cgi-bin/pdfs';

my $filename = "testpage.pdf";

chdir($filePath) or die "Failed to chdir $filePath - $!";

my $filesize = -s $filename;

# print full header
print "Content-disposition: attachment; filename=$filename\n";
print "Content-Length: $filesize\n";
print "Content-Type: application/pdf\n\n";

# open in binmode
open my $read,'<',$filename or die "Failed to read $filename - $!";       
binmode $read;
     
while (<$read>) {
print;
}

- Kevin, perl coder unexceptional!
 
Keith, her machine would actually say that the file was downloaded successfully, but Acrobat threw a fit on it.

Kevin, thank you, it looks like the file size was it..... I guess I got too close to the code and didn't see that I didn't have the correct path. Also, what do you mean by 'more secure'?

Thanks to everyone, it is much appreciated.

Jim
 
Jim,

Note I said: Your script could maybe be a bit more secure.

If that is the enire script then you are OK. Although I suspect you are doing something with all those other form fields. Form data should always be validated to make sure the type of data is recieved from the form that the script expects to recieve.

You should disable file uploading (see the CGI module documentation) since your script appears to not be going to be used for that purpose.

You don't use form data in any file/folder paths (at least not in the code you posted) so that is good.

These two lines:

Code:
if ($file eq "RegDShort") {
$filename = "Agreement.pdf";
}
if ($file eq "RegDLong") {
$filename = "RegDOffering.pdf";
}

should maybe be something like:

Code:
if ($file eq "RegDShort") {
   $filename = "Agreement.pdf";
}
elsif ($file eq "RegDLong") {
   $filename = "RegDOffering.pdf";
}
else {
   die "Invalid or missing filename. ";
}

- Kevin, perl coder unexceptional!
 
There will not be any uploads from here.... On another part of the site there will be uploads but only after they have logged in, and only to the users dir, which they will never see. They only see a form and it handles everything from there.

But, you are right that I cut some code for posting. The results of the form are emailed for tracking purposes, a legal requirement that I had to meet.

Thanks for everything!
Jim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top