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
    Sorry - web browsers won't let you do that for security reasons. If they did I could put up a page with a hidden file upload field to snag your /etc/password or whatever the equivalent would be on Windows. If you do find a way to do this please report it as a bug in your browser!

    -sam

    PS: What browser are you using in DOS? Did you perhaps mean Windows?

      I am trying this from IE 5. I edit the file in Windows environment using Notepad and then upload to Unix.

      I did read that auto upload is not possible because of security reasons but was wondering if there is a way around.

Re: Upload file from DOS to Unix
by Errto (Vicar) on Jun 19, 2007 at 02:19 UTC
    You could approach this as an HTML problem. Use the target attribute of the form so that when it is submitted the results of the upload CGI are shown in a different window or different frame. This would allow the user, having selected the file, to submit it multiple times. However, that initial selection must be done by the user - the browser will not give you a way around that.

      target="_blank" in the form made it work as I like

      Thanks a lot