I am facing an issue with a form that is designed to upload a single file image to a Perl script. The problem arises when the Perl script does not receive the file stream correctly, resulting in a 0-byte file size. I suspect that there might be an error in passing the right name/reference from the HTML input to the JavaScript formdata.append function. For now, I am steering clear of jQuery and XHR version 2 solutions as I strive to troubleshoot and resolve this issue.
HTML Input:
<input id="newimg" type="file" onchange="changeimg(this.value,dbtable,dbid);">
JavaScript:
function changeimg(filename, dbtable, dbid) {
// Ignore "c:\fakepath\" in filename
var filename = document.getElementById('newimg').files[0].name;
var formdata = new FormData();
formdata.append("Content-Type", "multipart/form-data");
formdata.append("dbtable", dbtable);
formdata.append("dbid", dbid);
formdata.append("filename", filename);
if (window.XMLHttpRequest) {
// Code for IE7+, Firefox, Chrome, Opera, Safari
var xhr = new XMLHttpRequest();
} else {
// Code for IE6, IE5
var xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
xhr.open("POST","/uploader.pl",true);
xhr.send(formdata);
}
Perl snippet currently being used:
#
my $upload_filehandle = $cgi->upload("filename");
#
open (UPLOADFILE, ">$newfile");
binmode UPLOADFILE;
while ( <$upload_filehandle> ) {
print UPLOADFILE;
}
close UPLOADFILE;
#