Gossamer Forum
Home : General : Perl Programming :

File Downloads...

Quote Reply
File Downloads...
Hi guys,

I'm trying to get a script to download a file through a Perl script. I've got it working perfectly, up to the part where it actually does the download;

Code:
print qq{Accept-Ranges: bytes\n};
print qq{Content-Transfer-Encoding: 'binary'\n};
print qq{Content-Length: $file_size\n};
print qq{Content-Type: $file_mime;\n};
print qq{Content-Name: "$file_name"\n};
print qq{Content-Disposition: attachment; filename="$path"\n\n};
open(FILE,$path);
binmode(FILE);
my $line;
while (defined ($line = <FILE>)) {
print $line;
}
close(FILE);

Basically, the problem I am having... is the actual name of the document itself. Currently, whenever you try and download... it comes up as "download[1].cgi" ... and I can't for the life of me, work out how to change it to the real filename :(

Probably something really stupid, so sorry in advance <G>

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] File Downloads... In reply to
I had a similar problem with forcing ASCII download. With experimentation, the following worked:

open(FILE, "$base/$filename") || &error("Cannot open file [$filename] for download");
my @content = <FILE>;
close(FILE);
print qq~Content-Type:application/x-download\n~;
print qq~Content-Disposition:attachment;filename="$filename"\n\n~;
print @content;

The fourth line may be what you need - with modification in terms of application type.

----
Cheers,

Dan
Founder and CEO

LionsGate Creative
GoodPassRobot
Magelln
Quote Reply
Re: [Andy] File Downloads... In reply to
You can see how Gossamer Forum does it from about line 581 onward in GForum/Post/View.pm
Quote Reply
Re: [dan] File Downloads... In reply to
Hi Dan,

Thanks for the reply. I'm really stumped about this. It download fine now... but Word just won't open it!

Code:
my $file_size = -s $path;

print qq{Accept-Ranges: bytes\n};
print qq{Content-Transfer-Encoding: binary\n};
print qq{Content-Length: $file_size\n};
# print qq{Content-Type: $file_mime;\n};
print qq{Content-Type:application/x-download\n};
print qq{Content-Disposition: attachment; filename="$file_name"\n\n};
open(FILE,$path);
binmode(FILE);
my $line;
while (defined ($line = <FILE>)) {
print $line;
}
close(FILE);

$path = full path to the document
$file_name = the name of the file (i.e test.doc).

Also, I'm doing a check to make sure that the file does exist, with;

Code:
unless (-e $path) {
..show an error, and die here.
}

This has been bugging me most of the day now :|

TIA

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] File Downloads... In reply to
Are you sure the content is being printed to the browser?

You aren't performing any error checks on your open() call and so you'll not know if it failing or not. You may be downloading a blank file.
Quote Reply
Re: [Gmail] File Downloads... In reply to
Hi,

Thanks for the reply. I'm using;

if (!-e $path) { print $IN->header(); print "Error: $path doesn't exist!!!"; exit; }

...which I know works, as I renamed the file, and it then gave an error when trying to download (as you would expect).

Its driving me mad Pirate

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] File Downloads... In reply to
But that won't catch other system errors such as permission denied etc. It's worth a try rather than pulling the remainder of your hair out.
Quote Reply
Re: [Andy] File Downloads... In reply to
I wonder about changing:

print qq{Content-Type:application/x-download\n};

to:

print qq{Content-Type:application/msword\n};

Can you attach the DOC file in this forum? I'd like to check it out.

----
Cheers,

Dan
Founder and CEO

LionsGate Creative
GoodPassRobot
Magelln
Quote Reply
Re: [dan] File Downloads... In reply to
ARGH... still not working Unsure

Code:
print $IN->header(
-type => $mimetypes->{$ext},
"Content-Disposition" => \("inline; filename=$file_path; size=$file_size"),
"Content-Length" => $file_size
);

I've tried loads of different combinations. It doesn't even load for me now (sits, and just hangs).

Any ideas? Unimpressed

TIA

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] File Downloads... In reply to
Andy what about this?

Code:
print $IN->header({ '-force' => 1,
'-type' => 'application/octet-stream',
'-Content-Disposition' => \"filename=$filename",
});


It's working perfect for me.

zaaron

---------
GetLokal Reviews

Last edited by:

zaaron: Feb 12, 2005, 4:41 AM
Quote Reply
Re: [zaaron] File Downloads... In reply to
Hi,

I've just realised the most stupid mistake. I had commented out the actual binmode file printing code!

I've now got;

Code:
my $file_size = -s $file_path;


if ($file_size < 10) {
print $IN->header();
print "The specified file could not be found. $file_path - type: $mimetypes->{$ext}";
exit;
}

binmode STDOUT;

print $IN->header({ '-force' => 1,
'-type' => 'application/octet-stream',
'-Content-Disposition' => \"filename=$destsite-$ID.doc",
});

open (F, "<$file_path") || die "ERROR: $!";
binmode F;
{
local $\;
while (read(F, my $chunk, 4096)) {
print $chunk;
}
}
close (F);

And it works a treat =)

Thanks guys.

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!