Created
January 20, 2014 20:40
-
-
Save rijvirajib/8528704 to your computer and use it in GitHub Desktop.
Perl asynchronous file upload script using iframes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> | |
<script> | |
$(document).ready(function () { | |
$("#files").change(function() { | |
$("#status").html('<br>Uploading...'); | |
$("#form1").submit(); | |
}); | |
}); | |
function callback(data) { | |
var json = JSON.parse(data); | |
$("#status").html('<br>Complete.') | |
var files = json.files; | |
$.each(json.files, function(index, value) { | |
$("#msg").append('<br><a href="/customUpload/uploads/'+ value + '">' + value + '</a>'); | |
}); | |
} | |
</script> | |
</head> | |
<body> | |
<form action="/cgi-bin/customUpload.pl" id="form1" name="form1" encType="multipart/form-data" method="post" target="hidden_frame" > | |
<input type="file" id="files" name="files" style="width:450" multiple> | |
<br> | |
<iframe name='hidden_frame' id="hidden_frame" style='display:none'></iframe> | |
<span id="status"></span> | |
<span id="msg"></span> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use warnings; | |
use strict; | |
use CGI; | |
use JSON; | |
use DateTime; | |
use Data::Dumper; | |
my $form = new CGI; | |
print $form->header; | |
my $web_home = "$ENV{DOCUMENT_ROOT}/customUpload"; | |
my @upload_fh = $form->upload("files"); | |
my @completed_files = (); | |
umask 000; | |
for my $UPLOAD_FH (@upload_fh) { | |
if (defined $UPLOAD_FH) { | |
my $dt = DateTime->now(); | |
my $new_file_name = "$dt-$UPLOAD_FH"; | |
open my $NEWFILE_FH, "+>", "$web_home/uploads/$new_file_name" | |
or die "Problems creating file '$UPLOAD_FH': $!"; | |
while ( <$UPLOAD_FH> ) { | |
print $NEWFILE_FH "$_"; | |
} | |
push (@completed_files, "$new_file_name"); | |
close $NEWFILE_FH or die "I cannot close filehandle: $!"; | |
} | |
} | |
my %response = ( | |
status => 200, | |
message => 'Files successfully uploaded.', | |
files => \@completed_files | |
); | |
my $json_string = JSON::encode_json \%response; | |
print "<script>parent.callback('".$json_string."')</script>"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment