Hello,
here is my code I'm using on server side::
var uploader = multer({
dest: 'upload/',
filename: function (req, file, cb) {
console.log('>>>>>>>>>>>>>>>>>>>', file);
cb( null, file.fieldname + '-' + Date.now() );
}
});
var uploadHandler = function(req, res, next){
if( req.url !== '/system/upload' || req.method !== 'POST') return next();
uploader.array('x-files')( req, res, function( err ){
console.log('???????????????', err, req.files, req.file);
} );
};
...
.use( uploadHandler )
On client side vanilla javascript:
upload: function( url, files ){
var self = this;
var formData = new FormData();
formData.append( 'x-files', Array.isArray(files)?files:[files] );
var request = new XMLHttpRequest();
request.open('POST', url, true);
request.send( formData );
}
The line
console.log('???????????????', err, req.files, req.file);
works, I see '???????????????' on the screen but err, req.files, req.file are all undefined and the filename function is never called and nothing is stored in the folder upload
No error, yet no files are stored or set in the req object...
I'm using [email protected]
Could you console.log req.headers before passing it to multer?
Sure, here it is:
{ host: 'localhost:8080',
'content-type': 'multipart/form-data; boundary=----WebKitFormBoundary0PmZBJD6r6CULTMU',
origin: 'http://localhost:8080',
'content-length': '154',
connection: 'keep-alive',
accept: '*/*',
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/600.7.12 (KHTML, like Gecko) Version/8.0.7 Safari/600.7.12',
referer: 'http://localhost:8080/work',
'accept-language': 'en-us',
'accept-encoding': 'gzip, deflate' }
Strangely enough, with version 0.1 it worked fine, I only refreshed to version v1 today...
Could you try the following code:
var upload = multer({ dest: 'upload/' }).array('x-files')
function uploadHandler (req, res, next) {
if (req.url !== '/system/upload' || req.method !== 'POST') return next()
console.log('STARTING UPLOAD')
upload(req, res, function (err) {
if (err) return next(err)
console.log('UPLOAD FINISHED')
console.log(req.files)
})
}
app.use(uploadHandler)
This issue have now been dormant for >1 month, please answer the last instructions I posted to get more help :)