Code check

K

Kati

I'm trying to get a PDF to download from a web location, instead of making
browsers display it with whatever PDF reader is configured to their
browser. So I tested the following code, placing the PDF file and the below
script in the cgi-bin:

#!/usr/bin/perl
print "Content-Type: application/force-download\n";
print "Content-Disposition: attachment\; filename=myFile.pdf\n";
print "Content-Length: 340\n";
print "Content-Description: PDF document\n\n";
open (myFile, "myFile.pdf");
print <myFile>

The above works, as far as it prompts browsers to download the file or
launch it in a selected PDF application.
However, after it is downloaded the file is corrupted for some reason and
will not open.
Could anyone who knows kindly point out what I might be doing wrong?
 
A

A. Sinan Unur

I'm trying to get a PDF to download from a web location, instead of
making browsers display it with whatever PDF reader is configured to
their browser. So I tested the following code, placing the PDF file
and the below script in the cgi-bin:

#!/usr/bin/perl

use strict;
use warnings;
$| = 1;
print "Content-Type: application/force-download\n";
print "Content-Disposition: attachment\; filename=myFile.pdf\n";
print "Content-Length: 340\n";

Is the file really 340 bytes in length?
print "Content-Description: PDF document\n\n";
open (myFile, "myFile.pdf");

Hmmmm ... useless use of double quotes and no error checking.

open MYFILE, '<', 'myFile.pdf' or die "Cannot open myFile.pdf: $!";
binmode MYFILE;
binmode STDOUT;
print <myFile>

Note that, MYFILE is conventionally preferred over myFile.
The above works, as far as it prompts browsers to download the file or
launch it in a selected PDF application.
However, after it is downloaded the file is corrupted for some reason
and will not open.
Could anyone who knows kindly point out what I might be doing wrong?

Make sure you are sending the correct length and you have called binmode
on both the input file handle and STDOUT.
 
A

A. Sinan Unur

A. Sinan Unur wrote:



I also posted a response, but I can see that you made several
corrections to the script that I missed. Is there a proper usenet way
to say that your answer is better than mine, or do I simply swallow my
pride and let the regulars on this newsgroup critique my code?

That is the benefit of fora such as this group. Many a time I will
suggest something which seems reasonable to me only to find out how much
better it could have been done. In any case, I agree with Randal that the
whole approach is rather distasteful, and I wish I had said that before.
So, you are not alone in having second thoughts :)

Sinan.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,145
Messages
2,570,826
Members
47,371
Latest member
Brkaa

Latest Threads

Top