I have to create an upload function where admins can upload a report for a certain user.
I have access to this data
- User ID (Number)
- Report Type (Number)
- Report (File)
When an an admin uploads a report, I want to create a directory if it doesn't exist yet like this
var location = __dirname + '/../../public/' + USERID + 'reports/' + filename;
I've been following this tutorial to upload a file and so this is the code I have
function uploadReport(req, res) {
var fstream;
var location = __dirname + '/../../public/reports/' + filename;
if (req.busboy) {
req.busboy.on('file', function (fieldname, file, filename, encoding, mimetype) {
fstream = fs.createWriteStream(location);
file.pipe(fstream);
fstream.on('close', function () {
console.log('file ' + filename + ' uploaded');
});
});
req.busboy.on('finish', function () {
res.json({
success: true
});
});
req.pipe(req.busboy);
}
}
this is what I sent from my front end
------WebKitFormBoundaryvS6HEnoxsgpmx646
Content-Disposition: form-data; name="participant"
1
------WebKitFormBoundaryvS6HEnoxsgpmx646
Content-Disposition: form-data; name="type"
1
------WebKitFormBoundaryvS6HEnoxsgpmx646
Content-Disposition: form-data; name="file"; filename="test.pdf"
Content-Type: application/pdf
------WebKitFormBoundaryvS6HEnoxsgpmx646--
How can I extract participant
and type
from my form data and put them into useable variables?