Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

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:

Hi,
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; } } xmlhttp.open("POST","../cgi-bin/upload.pl",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>
Thanks

Comment on Upload file- Ajax+Perl
Download Code
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
    Hi

    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 OptimalWorks.net instead using php

    Igael

      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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (9)
As of 2014-12-28 05:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (178 votes), past polls