Re: Copying A File From A Remote Location Through A Webpage

2002-08-30 Thread $Bill Luebkert

Barlow, Neil wrote:
> Hi All,
> 
> My problem is that I am trying to write a webpage in Perl that will allow
> the user to click on a verification button on the webpage and it will then
> copy down a file from a remote location to their machine?
> 
> I have setup the web directory so that the file to be downloaded is in a
> folder one directory down from the webpage.  I was planning on using the
> module File::Copy to do the copy from the webserver to the client, but it
> doesn't work for me as it says that the directory doesn't exist. 
> 
> Can anyone advise on how to do this properly?

Here is my download accumulated knowledge snippet:

use strict;

my $path = 'c:/rootdir';# example path to download files
my $file = 'image.gif'; # example (or get from args)
my $mimetype = 'image/gif'; # example (or create hash for .ext)
my $size = -s "$path/$file";# get file size
$size or die "$path/$file: empty file";

open IN, "$path/$file" or die "$path/$file download: $!";
binmode IN; # needed on Win32 for binary files

# these headers in one combination or another should handle the popup getting
# the right filename on the download (instead of script name)

print <;# this could be fine for text

# using block read/write instead of above print (better for binary data)

while (sysread (IN, my $buf, 4096)) {   # 4096 is arbitrary choice for blksize
syswrite STDOUT, $buf, length $buf;
}

close IN;

__END__


-- 
   ,-/-  __  _  _ $Bill Luebkert   ICQ=162126130
  (_/   /  )// //   DBE Collectibles   Mailto:[EMAIL PROTECTED]
   / ) /--<  o // //  http://dbecoll.tripod.com/ (Free site for Perl)
-/-' /___/_<_http://www.todbe.com/

___
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs



RE: Copying A File From A Remote Location Through A Webpage

2002-09-02 Thread Morse, Richard E.

Have you looked at the CGI.pm docs?

There are two parts to uploading docs via a webpage.  First, you have supply the
correct type of form on the webpage:






is probably the bare minimum that you need.

Then, the script needs to properly parse the sent file.  Below is a loop that I
use in a script I have.  You should probably customize this to fit your
needs

  foreach my $file_type ('resp', 'family', 'cancer') {
# first, we need to grab the uploaded file's file handle, and check
# to see if there was an error during the send
my $fh = $q->upload($file_type);
if (!$fh && $q->cgi_error) {
  die("File upload error for $file_type: " . $q->cgi_error);
} elsif (!$fh) {
  die("No file specified for $file_type");
}

# here, we make sure that the file name contains the $file_type string.
This
# is used as a simple stop-gap to try and make sure that the correct
file is being
# passed for each parameter...
my $in_file_name = $q->param($file_type);
$in_file_name =~ s/\\/\//g;
{
  # the uploaded file's name contains the entire remote path
  my $fname = pop @{[ split("/", $in_file_name) ]};
  unless ($fname =~ m/$file_type/i) {
die("File uploaded for file type '$file_type' was named
'$fname', which doesn't contain the string '$file_type'");
  }
}

# at this point, we have a good file that has been uploaded
open(my $out, ">", $save_dir . $file_type . '.dat') or die("Could not
create '$save_dir$file_type.dat': $!");

# just in case, although on unix boxes it shouldn't matter, we make sure
that
# we have raw character discipline
binmode($fh, ":raw");
binmode($out, ":raw");

# now, read the data using buffered IO (rather than sysread)
my $bytesread;
my $buffer;
while($bytesread = read($fh, $buffer, 1024)) {
  print $out $buffer;
}

# read returns undef on an error.  I don't know where this error gets
stored...
if (!defined($bytesread)) {
  die("An error occured while trying to write '$save_dir$file_type.dat':
$!");
}

# now close the files...
close($fh);
close($out);
  }

HTH,
Ricky
___
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs