Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
Dear Monks
I have an unique requirement with file upload but having difficulties to make it work.
I want to upload a text file from DOS to Unix, which is working fine using the "filefield" option in CGI.pm but my requirements is that I need to upload the file by selecting it once and then remember the file that was selected to do subsequent uploads.
The reason is that I use the uploaded text file to run some programs on the unix side and hence, I may have to modify the 'text' file (in windows) and upload that to unix multiple times. As is it really annoying to select the same file again and again, I would like to come up with a logic so that the user has to select the file once and for the subsequent uploads, he would have to only hit the 'submit' button. Unfortunately, I am not able to make it work for the 2nd part. Please find the code below. Please provide some suggestions as to how I can get that done
#! /usr/bin/perl -w use strict; use CGI::Carp 'fatalsToBrowser'; use CGI qw/:standard/; use lib '/path/to/lib'; my $query = new CGI; # Using 'filefield' option. THIS WORKS FINE!!! #my $dosfilename = $query->param('upfile') || "None"; # If I send the filename as 'hidden' field, then I can retrieve that i +n subsequent runs. The variable will have the path to DOS file as sho +wn below:- # THIS DOESN'T WORK my $dosfilename = 'C:\\Documents and Settings\\user\\Desktop\\Testing\ +\file_name.txt'; print $query->header( ); print $query->start_html(); print $query->start_multipart_form( -name => 'data', -method => 'post' +); print $query->p( $dosfilename ); if ($dosfilename ne 'None' ) { my $unixfilename = form_unix_filename($dosfilename); my ($ret_stat, $bytes_read) = uploadfiletounix($dosfilename, $unixfile +name); print $query->p( $ret_stat ); print $query->p( $bytes_read ); } print $query->filefield( -name => 'upfile' ); print $query->br(); print $query->submit( -name => 'Submit' ); print $query->end_form(); print $query->end_html(); sub form_unix_filename { my $dosfilename = shift; my $unixfilename = substr $dosfilename, rindex( $dosfilename, '\\' + ) + 1; # replace any 'white spaces' with '_' $unixfilename =~ s/\s/_/g; return $unixfilename; } sub uploadfiletounix{ my ( $dosfilename, $unixfilename ) = @_; print $query->p( $unixfilename ); my ( $buffer, $bytes, $read ); open ( OUTFILE, ">", $unixfilename ) || return "Failed"; #die("Unable to create upload file in the server: $!\n"); + # set binary mode for the input file and the output file binmode $dosfilename; binmode OUTFILE; # Read the dos file and write to the unix file # while( $read = read( $dosfilename, $buffer, 16384 ) ) { # Write to o/p file print OUTFILE $buffer; # Keep track of the no of bytes read $bytes += $read; } # Close the o/p file close OUTFILE; # Return success return ( "Success", $bytes ); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Upload file from DOS to Unix
by samtregar (Abbot) on Jun 18, 2007 at 22:16 UTC | |
by Joost (Canon) on Jun 18, 2007 at 22:24 UTC | |
by Anonymous Monk on Jun 19, 2007 at 01:05 UTC | |
|
Re: Upload file from DOS to Unix
by Errto (Vicar) on Jun 19, 2007 at 02:19 UTC | |
by Anonymous Monk on Jun 19, 2007 at 22:13 UTC |