Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks!
I found this code here and it supposed to do what I am looking for, but I can not get it to work at all. Anyone here got it to work or have any better example?
Here is what I have: I have jquery-1.2.6.js downloaded
HTML
html> <head> <!--jquery should be included before any other js--> <script type="text/javascript" language="javascript" src="jquery/jquer +y-1.2.6.js"></script> <script type="text/javascript"> $(document).ready(function () { $("#file").change(function() { $("#form1").submit(); }); }); function callback(msg) { $("#msg").html(msg); } </script> </head> <body> <form action="../cgi-bin/ajax_upload.pl" id="form1" name="form1" encty +pe="multipart/form-data" method="post" target="hidden_frame" > <input type="file" id="file" name="file" style="width:450"> <INPUT type="submit" id="test" value="submit"> <span id="msg"></span> <br> <iframe name='hidden_frame' id="hidden_frame" style='display:none'></i +frame> </form> </body> </html>
Perl
#!/usr/bin/perl use warnings; use strict; use CGI; my $form = new CGI; print $form->header; #Print HTML header. this is mandatory #my $web_home = "$ENV{DOCUMENT_ROOT}/ajax_files"; my $web_home = "../../ajax_files"; my $UPLOAD_FH = $form->upload("uploadfile"); my $newfilename = "new_file"; umask 0000; #This is needed to ensure permission in new file open my $NEWFILE_FH, "+>", "$web_home/tmp/$newfilename.txt" or die "Problems creating file '$newfilename': $!"; while ( <$UPLOAD_FH> ) { print $NEWFILE_FH "$_"; } close $NEWFILE_FH or die "I cannot close filehandle: $!"; ##this is the only way to send msg back to the client print "<script>parent.callback('upload file success')</script>"; exit;
Thanks for the Help!!

Replies are listed 'Best First'.
Re: Perl Upload with JQuery
by marto (Cardinal) on Dec 08, 2010 at 15:12 UTC

    What happened when you tried it? What does your webservers error log say?

    my $UPLOAD_FH = $form->upload("uploadfile");

    You don't have a field named uploadfile on the form. Perhaps go back to basics and read Ovid's CGI Course.

      Sorry I do, I just typed it wrong here, but this is what I have:
      my $UPLOAD_FH = $form->upload("file");
      And there is no error anywhere, I cant understand why!

        In future please copy and paste exactly what you have, rather than allow potential typos, otherwise we could be here all day fixing problems you don't actually have in your code.

        "I can not get it to work at all"

        This isn't a reasonable way to explain how you have tried to run this or debug it. See Ovids course I linked to previously regarding debugging your script. It has lots of advice there, saving me for retyping it here. In addition to that consider using something like Friebug do aid debugging web based tools.

        A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Perl Upload with JQuery
by ww (Archbishop) on Dec 08, 2010 at 18:51 UTC

    Line 20 of your Perl creates a handle for a file named "new_filetxt" -- not a handle for a file named "new_file.txt" (i.e. filename dot extension) as you apparently intended.

    Inside (despite!) the double quotes, open my $NEWFILE_FH, "+>", "$web_home/tmp/$newfilename.txt" concatenates "txt" to "new_file":

    perl -e "my $filename = "new_file"; my $var ="$filename.txt"; print $v +ar;" new_filetxt

    And the error message in "or die "Problems creating file '$newfilename': $!"; misleadingly reports an inability to create a file of a different name, "new_file."

    Update: Fixed typo in para1, s/"new_filetext"/"new_filetxt"/;

    Update2: My VERY bad; tested at a w32 CLI and forgot that Mr. Gates prodigy would not correct my failure to escape the interior double quotes. Sorry for inconveniencing all those electrons... and for my error!

      I am sending to this other Perl script from the html form, but still can't get the file to upload:
      #!/usr/bin/perl -w use strict; use CGI; use CGI::Carp qw ( fatalsToBrowser ); use File::Basename; my $safe_filename_characters = "a-zA-Z0-9_.-"; my $upload_dir = "/upfiles"; my $query = new CGI; my $filename = $query->param("file"); my ( $name, $path, $extension ) = fileparse ( $filename, '\..*' ); $filename = $name . $extension; $filename =~ tr/ /_/; $filename =~ s/[^$safe_filename_characters]//g; if ( $filename =~ /^([$safe_filename_characters]+)$/ ) { $filename = $1; } else { die "Filename contains invalid characters"; } my $upload_filehandle = $query->upload("file"); open ( UPLOADFILE, ">$upload_dir/$filename" ) or die "$!"; while ( <$upload_filehandle> ) { print UPLOADFILE "$_"; } close UPLOADFILE; ##this is the only way to send msg back to the client print "<script>parent.callback('upload file success')</script>"; exit;