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 MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

download files

Status
Not open for further replies.

imad77

Instructor
Oct 18, 2008
97
CA
Hi,

I experience some difficulties to perform a script to be able to download some files via HTTP.

The Perl script is located on a Linux in a server ("/var/ where I can run it via Internet Explorer.
I'm able to display the directory content ("/home/toto") but I'm not able to open or save the files from this directory to my local machine (Windows XP by example).

My goal is to be able to save a file when I click on them and choose the target on my local machine.

Can someone help me to fix this script? thanks in advance.

==========
#!/usr/bin/perl
use CGI;

$|=1;

my $query=new CGI;

print qq~
<table align="center" width="90%">
<tr><td><font color="black" face="Arial" size="4"><b>Files in current directory</b></td></tr>
<tr>
<td valign="top">
<font color="black" face="Arial" size="2">
~;
$dir="/home/toto";
opendir(DIR, "$dir");
@current = readdir(DIR);
closedir(DIR);


foreach(@current){
unless($_ eq '.' || $_ eq '..' || -d qq~$dir/$_~){
push(@currentfiles, $_);
}
}
@currentfiles = sort { uc($a) cmp uc($b) } @currentfiles;
for($aa = 0; $aa <= $#currentfiles; $aa++){
print qq~


<a href="$dir/$currentfiles[$aa]" target="_blank">$currentfiles[$aa]</a><a href="$dir/$currentfiles[$aa]"> </a></font><br>
~;
}
print qq~
</font></tr>
</table>
 
Have you tried to write any code yet to handle the download part your requirements? It really just consists of printing out the proper set of http headers that open a save-as dialog box.

Your code appears as though it should not work as there has been no http header printed before printing other output to the browser from your perl program.

my $query=new CGI;
print $query->header;

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Hi,

I tried another script to perform the same action, I'm able to get the save-as dialog but for one file. I would click on a file from a list and get the save-as dialog.

Here is the script:

#!/usr/bin/perl -T

use strict;
use warnings;
use CGI;
my $path_to_files = '/home/toto';

my $q = CGI->new;

my $file = $q->param('file') or error('Error: No file selected.');

if ($file =~ /^(\w+[\w.-]+\.\w+)$/) {
$file = $1;
}
else {
error('Error: Unexpected characters in filename.');
}

if ($file) {
download($file) or error('Error: an unknown error has occured. Try again.');
}

sub download {

open(DLFILE, '<', "$path_to_files/$file") or return(0);

print $q->header(-type => 'application/x-download',
-attachment => $file,
'Content-length' => -s "$path_to_files/$file",
);

binmode DLFILE;
print while <DLFILE>;
close (DLFILE);


}
 
Well, thats how the code works, it downloads one file.
Thats code I wrote (or part of some code I wrote since the error() function is not there) but I don't remember where I posted it.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Yes it downloads one file that I have to specify in the program :
my $file="abook.doc";
And I comment this line
:
#my $file = $q->param('file') or error('Error: No file selected.');

Because I get this error message: 'Error: No file selected.


here is the entire script:

#!/usr/bin/perl -T

use strict;
use warnings;
use CGI;
my $path_to_files = '/var/
my $q = CGI->new;

my $file="abook.doc";
#my $file = $q->param('file') or error('Error: No file selected.');

if ($file =~ /^(\w+[\w.-]+\.\w+)$/) {
$file = $1;
}
else {
error('Error: Unexpected characters in filename.');
}

if ($file) {
download($file) or error('Error: an unknown error has occured. Try again.');
}

sub download {

open(DLFILE, '<', "$path_to_files") or return(0);

print $q->header(-type => 'application/x-download',
-attachment => $file,
'Content-length' => -s "$path_to_files/$file",
);

binmode DLFILE;
print while <DLFILE>;
close (DLFILE);


}

sub error {
print $q->header(),
$q->start_html(-title=>'Error'),
$q->h1($_[0]),
$q->end_html;
exit(0);
}
 
I'm not totally sure what you are trying to do but in the first code you posted, these lines:

Code:
@currentfiles = sort { uc($a) cmp uc($b) } @currentfiles;
for($aa = 0; $aa <= $#currentfiles; $aa++){
print qq~


<a href="$dir/$currentfiles[$aa]" target="_blank">$currentfiles[$aa]</a><a href="$dir/$currentfiles[$aa]"> </a></font><br>
~;
}

Should maybe be:

Code:
@currentfiles = sort { uc($a) cmp uc($b) } @currentfiles;
for(@currentfiles){
print qq~
<a href="cgi-bin/download.pl?file=$_" target="_blank">$_</a><br>
~;
}

Where download.pl is the name of the download script and assumes its in the cgi-bin.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Also...

put this line back in the code:

Code:
my $file = $q->param('file') or error('Error: No file selected.');

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
When I run this script (see below), I get this error message:
Error: No file selected.


#!/usr/bin/perl -T

use strict;
use warnings;
use CGI;
my $path_to_files = '/var/
my $q = CGI->new;

my $file = $q->param('file') or error('Error: No file selected.');

if ($file =~ /^(\w+[\w.-]+\.\w+)$/) {
$file = $1;
}
else {
error('Error: Unexpected characters in filename.');
}

if ($file) {
download($file) or error('Error: an unknown error has occured. Try again.');
}

sub download {


open(DLFILE, '<', "$path_to_files") or return(0);

print $q->header(-type => 'application/x-download',
-attachment => $file,
'Content-length' => -s "$path_to_files/$file"
);

binmode DLFILE;
print while <DLFILE>;
close (DLFILE);


}

sub error {
print $q->header(),
$q->start_html(-title=>'Error'),
$q->h1($_[0]),
$q->end_html;
exit(0);
}
 
How are you trying to run the script? Are you running it by clicking on one of the links your other code outputs?

<a href="cgi-bin/download.pl?file=$_" target="_blank">$_</a>



------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Hi Kevin,

I run this script named download.pl from the web browser as:
I get this error message:
Error: No file selected.
I'm a newbie in this kind of programming.


Here is the content of this script:
======================
#!/usr/bin/perl -T

use strict;
use warnings;
use CGI;
my $path_to_files = '/var/
my $q = CGI->new;

my $file = $q->param('file') or error('Error: No file selected.');

if ($file =~ /^(\w+[\w.-]+\.\w+)$/) {
$file = $1;
}
else {
error('Error: Unexpected characters in filename.');
}

if ($file) {
download($file) or error('Error: an unknown error has occured. Try again.');
}

sub download {


open(DLFILE, '<', "$path_to_files") or return(0);

print $q->header(-type => 'application/x-download',
-attachment => $file,
'Content-length' => -s "$path_to_files/$file"
);

binmode DLFILE;
print while <DLFILE>;
close (DLFILE);


}

sub error {
print $q->header(),
$q->start_html(-title=>'Error'),
$q->h1($_[0]),
$q->end_html;
exit(0);
}


Thanks
 
What was all that first code that you posted for? I thought it was to generate the hyper links for the file you wanted to download. You run that code with the modifications I suggested then click one of the links it creates. It should make links something like this:

<a href="cgi-bin/download.pl?file=frog.jpg">frog.jpg</a>

If you wanted to run the code without the script you posted in your first post:


where filename is the name of a file in the /var/ directory that you want to download.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Hi Kevin,

When I run the code without the script I posted in my first post, it works fine for one file:



But I want to display a directory content and if I click on a hyper link for a file, I want to get a save-as dialog box.

Here is the first script download1.pl, I changed the line that you suggested to me, but it does not work and the directory content is not displayed.

=====================================
#!/usr/bin/perl
use CGI;

#$|=1;

my $query=new CGI;
print $query->header;

print $query->start_html(
-bgcolor=>"#ffffff",
-link=>"#ffff00",
-vlink=>"#00ffff",
-alink=>"#ffff00",
-text=>"#000000");


print qq~
<table align="center" width="90%">
<tr><td><font color="black" face="Arial" size="4"><b>Files in current directory</b></td></tr>
<tr>
<td valign="top">
<font color="black" face="Arial" size="2">
~;
$dir=".";
opendir(DIR, "$dir");
@current = readdir(DIR);
closedir(DIR);


foreach(@current){
unless($_ eq '.' || $_ eq '..' || -d qq~$dir/$_~){
push(@currentfiles, $_);
}
}
@currentfiles = sort { uc($a) cmp uc($b) } @currentfiles;
for($aa = 0; $aa <= $#currentfiles; $aa++){
print qq~

<a href="cgi-bin/download1.pl?file=$_" target="_blank">$_</a>

~;
}
print qq~
</font></tr>
</table>
</html>~;
=====================================
Have I to use both scripts?
 
Hi Kevin,

Now it works fine, I understand your suggestions, I performed these changes in download1.pl:

<a href="download.pl?file=$currentfiles[$aa]" target="_blank">$currentfiles[$aa]</a><a href="cgi-bin/download.pl?file=$currentfiles[$aa]"> </a></font><br>

It works fine

Thanks a lot for your help.

Here is the first script download0.pl:

========================

#!/usr/bin/perl
use CGI;

#$|=1;

my $query=new CGI;
print $query->header;

print $query->start_html(
-bgcolor=>"#ffffff",
-link=>"#ffff00",
-vlink=>"#00ffff",
-alink=>"#ffff00",
-text=>"#000000");


print qq~
<table align="center" width="90%">
<tr><td><font color="black" face="Arial" size="4"><b>Files in current directory</b></td></tr>
<tr>
<td valign="top">
<font color="black" face="Arial" size="2">
~;
$dir=".";
opendir(DIR, "$dir");
@current = readdir(DIR);
closedir(DIR);


foreach(@current){
unless($_ eq '.' || $_ eq '..' || -d qq~$dir/$_~){
push(@currentfiles, $_);
}
}
@currentfiles = sort { uc($a) cmp uc($b) } @currentfiles;
for($aa = 0; $aa <= $#currentfiles; $aa++){
print qq~
<a href="download.pl?file=$currentfiles[$aa]" target="_blank">$currentfiles[$aa]</a><a href="cgi-bin/download.pl?file=$currentfiles[$aa]"> </a></font><br>

~;
}
print qq~
</font></tr>
</table>
</html>~;

==============

Here is the second script download.pl:

#!/usr/bin/perl -T
use strict;
use warnings;
use CGI;
my $path_to_files = '/var/my $q = CGI->new;
#my $file="abook.doc";
my $file = $q->param('file') or error('Error: No file selected.');
if ($file =~ /^(\w+[\w.-]+\.\w+)$/) {
$file = $1;
}
else {
error('Error: Unexpected characters in filename.');
}
if ($file) {
download($file) or error('Error: an unknown error has occured. Try again.');
}
sub download {

open(DLFILE, '<', "$path_to_files") or return(0);
print $q->header(-type => 'application/x-download',
-attachment => $file,
'Content-length' => -s "$path_to_files/$file"
);
binmode DLFILE;
print while <DLFILE>;
close (DLFILE);

}
sub error {
print $q->header(),
$q->start_html(-title=>'Error'),
$q->h1($_[0]),
$q->end_html;
exit(0);
}



Thanks for your great help
 
OK, very good [smile]

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Hi Kevin,

When I click on the link, I get another IE Window and save-as box.
Have you any idea to avoid this second IE Window?

Thanks a lot.
 
remove this from the links:

target="_blank"

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Oh thanks for your great help. It woks fine.

thanks a lot
 
You're welcome

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Hi Kevin,

When I run these scripts, I get the direcory content and I'm able to click on the files and get the save-as box and all work fine.

I changed a part of the first script where I deleted :
"-d qq~$dir/$_~"

Now the script list the subdirectories and files :

foreach(@current){
unless($_ eq '.' || $_ eq '..'){
push(@currentfiles, $_);
}


But when I click on a subdirectory I get this error message:
'Error: Unexpected characters in filename'

I'm not able to display the content of this subdirectory.

Is it a way to fix this issue?

Thanks for your great help.
 
When you click a link it runs the download.pl script which wants to download a file, not open and display the contents of a subdirectory. If you want to browse the subdirectories to display the contents thats a different script.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top