Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Upload file- Ajax+Perl

by Anonymous Monk
on Oct 12, 2012 at 17:44 UTC ( #998757=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I would like to upload file using Ajax+Perl. I am not able to send data to server using Ajax.
Can anyone please help me to upload file content to Perl program using Ajax.
Here is my HTML code
<html> <head> <title> New Document </title> </head> <script type="text/javascript"> <!-- function UploadStatus() { var xmlhttp; if (window.XMLHttpRequest) {// It will support IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// It will support IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("statushere").innerHTML=xmlhttp.responseTe +xt; } }"POST","../cgi-bin/",true); xmlhttp.send(); } //--> </script> <body> <h1>Upload Form</h1> <form enctype="multipart/form-data"> <center> Enter a file to upload: <input type="file" name="upfile"><br> <input type="button" name="button" value="Upload File" onclick="Upload +Status()"> </center> <div id="statushere" align="center"></div> </form> </body> </html>

Comment on Upload file- Ajax+Perl
Download Code
Replies are listed 'Best First'.
Re: Upload file- Ajax+Perl
by Anonymous Monk on Oct 12, 2012 at 21:47 UTC
      PHP+Ajax File upload examples are available in net. Any Perl+Ajax script available to upload images to server?
Re: Upload file- Ajax+Perl
by Anonymous Monk on Jan 29, 2014 at 14:55 UTC

    for ajax file upload : keep in mind that you can get your file by reading STDIN ( without calling CGI , don't include it ). Send the other informations as HTTP headers.

    my $fn = (exists $ENV{'HTTP_X_FILENAME'} ? $ENV{'HTTP_X_FILENAME'} + : "" ); my $rep = (exists $ENV{'HTTP_X_REP'} ? $ENV{'HTTP_X_REP'} : "" ); print "Content-type: text/html; charset=utf-8\r\n\r\n" ; my $repTmp = "/tmp/" ; my $fichier = "$repTmp/$fn" ; my $len = 0 ; if( open ( UPLOADFILE, ">$fichier" ) ) { binmode UPLOADFILE; while( <STDIN> ) { print UPLOADFILE $_ ; $len += length($_); } close UPLOADFILE ; } # an internal function to verify the file and to move it fais_copy($repTmp , $fn,$rep,1,0); print "ok $rep$fn de $len octets\n" ;

    For multiple files, an AJAX post makes several calls to the same cgi. Then it is ok.

    I made this tiny script to handle the filedrag.js by Craig Buckler (@craigbuckler) of instead using php


      for ajax file upload : keep in mind that you can get your file by reading STDIN

      You can also make a nice hat by stabbing yourself with a knife, doesn't make it a good idea

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://998757]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (8)
As of 2015-11-25 10:48 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (673 votes), past polls