I have the following Ajax upload (Base64 canvas data from cropper.js), so the Ajax upload is not from a form.
$('.gogo').on('touchstart mousedown', function(e){
e.preventDefault();
e.stopPropagation();
var img = new Array();
img[0] = fetchCrop1();
img[1] = fetchCrop2();
$.ajax({
cache: false,
type : 'POST',
url : 'http://localhost:3001/img',
data: { img : img },
contentType: false,
processData: false,
success : function(data) {
console.log('AJAX: '+data);
}
});
});
and the Multer script from examples that I have seen
var storage = multer.diskStorage({
destination: function (req, file, callback) {
callback(null, '/img');
},
filename: function (req, file, callback) {
callback(null, file.fieldname + '-' + Date.now());
}
});
app.post('/img',function(req,res){
var upload = multer({ storage : storage }).array('img',2);
upload(req,res,function(err) {
console.log(req.body);
console.log(req.files);
if(err) {
return res.end("Error uploading file.");
}
res.end("File is uploaded");
});
});
I have read numerous post with little luck as I keep getting req.body and req.files as {} and undefined.
I have tried different contentType, and single uploads but clearly I am missing something, or JQuery Ajax doesn't work with Multer multiple files?
I am relatively new to Node (LAMP man) and do find the JavaScript syntax a little alien.
I just want to upload two Base64 images to the server /img folder.
The Base64 is working fine from cropper.js I believe, code below for that function
function fetchCrop1(){
/* Note that images greater than 1000px are rejected by the server */
var $image = $('#image');
result = $image.cropper("getCroppedCanvas", "{ "width": 1000,
"height": 700 }", '');
$(document).find('#dataImg').val(result.toDataURL('image/jpeg'));
$('#dataImg').attr('value');
$('#download').attr('href', result.toDataURL('image/jpeg'));
return $('#dataImg').attr('value');
}