Si al final he entendido mejor como funciona y encontre una solucions.
Aqui les dejo un ejemplo muy sencillo de como utilitzar ajax and cgi/perl para subir un archivo.
No hay recargas de la web ni boton de envio.
Espero q tambien pueda servir de ejemplo para otros.
Gracias por vuestra ayuda
HTML
Código:
<!--jquery should be included before any other js-->
<script type="text/javascript" language="javascript" src="/simple_ajax_cgi_example/jquery-1.2.6.js"></script>
<script type="text/javascript" language="javascript" src="/simple_ajax_cgi_example/simple_ajax_cgi_example.js"></script>
<html>
<body>
<form action="/cgi-bin/simple_ajax_cgi_example.cgi" id="form1" name="form1" encType="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'></iframe>
</form>
</body>
</html>
AJAX simple_ajax_cgi_example.js
Código:
$(document).ready(function () {
$("#file").change(function() {
$("#form1").submit();
});
});
function callback(msg) {
$("#msg").html(msg);
}
CGI/Perl simple_ajax_cgi_example.js
Código:
#!/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}/simple_ajax_cgi_example";
my $UPLOAD_FH = $form->upload("file");
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;