Hi all,

I have a problem using the upload method from CGI. I am getting an undefined filehandle
Use of uninitialized value in <HANDLE>
I don't have problems retrieving the parameters from the ajax and creating the folder and the file (an empty one)
The problem is I cannot read from the filehandle.
Any suggestions how to solve this.

Thanks for your time and help!

html form

<link rel="stylesheet" href="jquery.jgrowl.css" type="text/css"/> <!-- jQuery Library --> <script type="text/javascript" language="javascript" src="jquery-1.2.6 +.js"></script> <script type="text/javascript" language="javascript" src="jquery.jgrow +l.js"></script> <script type="text/javascript" language="javascript" src="test_ajax.js +"></script> <input type=file id=test> <div id=output></div>

ajax

$(document).ready(function () { $("#test").change(function(){ alert($(this).val()); var info = $.trim($(this).val()); $.ajax({ type: "POST", url: "/cgi-bin/upload_and_check.cgi", data: "filename="+info+"&session_id=abc", success: function(msg){ alert("This is the message: " + msg); $("#output").html(msg); $.jGrowl(msg, { sticky: true }); } }); }); });

CGI

#!/usr/bin/perl use warnings; use strict; use CGI; my $form = new CGI; print $form->header; #Print HTML header my $web_home = "$ENV{DOCUMENT_ROOT}/ajax"; #Getting parametres from form my $session_id = $form->param("session_id"); my $filename = $form->param("filename"); #Create temp dir if doesn't exist yet umask 0000; if ( !-e "$web_home/tmp/$session_id" ) { mkdir "$web_home/tmp/$session_id", 0777 or die "Problems creating temporary dir '$web_home/tmp/$sessi +on_id': $!\n"; } #the upload() method to grab the file handle my $UPLOAD_FH = $form->upload("filename"); my $newfilename = $session_id; open my $NEWFILE_FH, "+>", "$web_home/tmp/$session_id/$newfilename.txt +" or die "Problems creating file '$newfilename': $!"; while ( <$UPLOAD_FH> ) { print $NEWFILE_FH; } close $NEWFILE_FH or die "I cannot close filehandle: $!"; exit;

In reply to CGI upload from ajax by flope004

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.