Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Perl (http://www.go4expert.com/forums/perl/)
-   -   Download a file from webserver to local computer using browser (http://www.go4expert.com/forums/download-file-webserver-local-computer-t1431/)

Rakish 20Sep2006 01:12

Download a file from webserver to local computer using browser
 
Hi all,
Thanks for sharing your knowledge in the past,

I want to allow user to download a file on the webserver to their local system using Just a browser.

the file readme.txt is stored in /tmp folder.

please tell me how can i implement it.

Thanks yall,

Rakesh

shabbir 20Sep2006 10:37

Re: Download a file from webserver to local computer using browser
 
Just putting that in the web server directory will allow it to download or you can use any server side script even to achieve the same. in PHP it would be something like
Code: PHP

header("Content-Length: $size");
header("Content-Type: $type");
header('Cache-Control: no-cache, must-revalidate');           // HTTP/1.1
header('Pragma: no-cache');                                   // HTTP/1.0
header('Expires: Mon, 26 Dec 1979 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Content-transfer-encoding: binary');

if(stristr($type,"zip"))
{
    header("Content-Disposition: attachment; filename=\"$name\"");
}
else if(stristr($type,"image"))
{
    header("Content-Disposition: inline; filename=\"$name\"");
}
echo $bin_data;
exit;


Rakish 22Sep2006 22:47

Re: Download a file from webserver to local computer using browser
 
Thanks shabbir,
but I have a a link like:
<a href=http://xyz.com/FileName="abc.zip"> abc.zip</a>

and when the user clicks on abc.zip hyper link, the FileName is passed to download script, which triggers the automatic download process(save as popup).

I dont know how to do this.

have any idea?
Thanks

Rakish 23Sep2006 00:56

Re: Download a file from webserver to local computer using browser
 
This is what I have and am trying to do.
the file name is passed to this script as a query string
Code:


if (length ($ENV{'QUERY_STRING'}) > 0){
      $buffer = $ENV{'QUERY_STRING'};
      @pairs = split(/&/, $buffer);
      foreach $pair (@pairs){
          ($name, $value) = split(/=/, $pair);
          $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
          $in{$name} = $value;
      }
 }

$filename=$in{'FileName'};
my $files_location; 

$files_location = "/tmp";

if ($filename eq '') { 

print "You must specify a file to download."; 
} else {

my $data="";
open(DLFILE, "<$files_location/$filename") || Error('open', 'file');


while(<DLFILE>) { $data .= $_; }
close(DLFILE) || Error ('close', 'file'); 

print "Content-Type: application/x-unknown\r\n";
print "Content-Disposition: attachment;$filename\r\n";
print "Content-length: ", length($data), "\r\n\r\n$data";

}

but i dont know how to prevent the data from being read by the browser, as if i try to download a .zip file, the browser displays weird characthers.

What i am trying to do is to let user save the file where ever they want with the

Save As pop-up


Please help,
Rakesh


All times are GMT +5.5. The time now is 21:11.