Reputation: 1894
I'm trying to upload a file to my node server. Here's my client-side code:
// data is {_id: 'somebigstring', file: an ArrayBuffer}
api.addDocument = function (data, cb){
// $.post(BASE_PATH + '/requests/addDocument', data, cb);
var formData = new FormData();
_.keys(data).forEach(key => {
formData.append(key, data[key]);
})
$.ajax({
type: 'POST',
url: BASE_PATH + '/requests/addDocument',
data: formData,
processData: false,
contentType: false
}).done(cb);
}
Here's what I'm doing server-side (in Node and Express):
app.use(multer({dest:'./uploads/'}));
app.use(function (req, res, next){
debugger;
next();
});
At the debugger
line, I check req.body
and get this:
{ _id: '555bcc06798e6f9c69b461e0',
file: '[object ArrayBuffer]' }
How do I get the actual file, rather than '[object ArrayBuffer]'
?
Upvotes: 1
Views: 1470
Reputation: 106736
form.append()
only supports values that are one of: string, File, or Blob. If the value is not one of those, it is converted to a string (this is why you see the result of .toString()
as the value for your file
).
So one solution might be to instead do:
_.keys(data).forEach(key => {
var val = data[key];
if (val instanceof ArrayBuffer)
val = new Blob([val]);
formData.append(key, val);
})
Upvotes: 3