I was wondering if someone knows how to extract the file content from an uploaded file when using express/nodejs
I have the following code and it's clear how to pipe the input to a filestream, but how do I deserialize the upload to a plain javascript object? The purpose is to extract information from the uploaded file and use it somewhere else. Ideally I don't want to go via a temporary file on disk. The format of the file is json.
app.post("/upload", function(req, res){
req.pipe(req.busboy);
req.busboy.on('file', function (fieldname, file, filename) {
console.log("Uploading: " + filename);
console.log(file);
console.log(fieldname);
// I don't want to go via a file, but straight to a JS object
//fstream = fs.createWriteStream('/files/' + filename);
//
//file.pipe(fstream);
//
//fstream.on('close', function () {
//
// res.redirect('back');
//
//});
});
});
The file upload is triggered like this:
var formData = new FormData($('form')[0]);
e.preventDefault();
$.ajax({
url: 'upload',
type: 'POST',
data: formData,
cache: false,
dataType:'json',
contentType: false,
enctype: 'multipart/form-data',
processData: false,
success: function (response) {
}
});