Busboy is the middleware I'm using to upload a file. Using an html form inside Chrome, I can upload files (using the 'file' event) but when an android client tries to upload a file, it doesn't trigger the 'file' event, it triggers the 'field' event instead.
Here the code snippet I am using on the server side:
import express from 'express';
import busboy from 'connect-busboy';
const app = express();
const busUpload = (req, res)=> {
req.busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
saveTo = `${destination}/${filename}`;
Log('uploading to', saveTo);
file.pipe(fs.createWriteStream(saveTo));
// file is saved successfully.
});
req.busboy.on('field', function(key, value, keyTruncated, valueTruncated) {
// I guess 'value' contains the file, but how do I save it? what is the name of file?
});
req.busboy.on('finish', function() {
Log('upload completed');
// res.writeHead(200, {'Connection': 'close'});
res.json({sucess: true});
});
// req.pipe(req.busboy);
};
app.use('/uploads', busboy({immediate: true}), busUpload)
What's the difference? What should I tell the android developer to change in his request? Or how can I save the file inside the handler of the 'field' event?
value
and it seems to be the buffer of the file which I don't think I can get the filename out of it. usingformidable
instead ofbusboy
, and doingconsole.log({fields, files})
I could understand that when I upload from browser, there is a keyfilename
(as in<input name="filename"
) insidefiles
but when I upload from android, the key is not infiles
but is infields
so I guess android is sending the file withouttype="file"
in theinput
field. it's like it's a binary which comes in atext
field. and it seems that's why busboy doesn't find the file. – Clorindaclorinde