3

Is it possible to send additionally to the file and json object containing data with multer? I found this thread. But it only explains how to attach one field at the time.

Here what i have currently on client side:

request
  .post(uploadPOSTUrl)
  .set('Accept', 'application/json')
  .field('Test', object.TestField)
  .attach('file', file)
  .end((err, res) => {
    if (err) {

    } else {

    }
  });

and server side

 export function upload(req, res){
    console.log("UploadedJSON: ", req.body);
    console.log("UploadedFile: ",req.file); 
    res.status(204).end();
}

but instead of just sending 1 field. I need to send the whole object .field('Test', object). When i do this, i receive [Object object] on the server side and can't access the fields.

My only solution right now would be to loop and add .field() for every field in my object...

Nocturno
  • 9,579
  • 5
  • 31
  • 39
Daniel Storch
  • 979
  • 3
  • 10
  • 25

1 Answers1

2

Your client-side code looks like it uses the SuperAgent library, am I right? If so, the real question is how to send multipart requests using SuperAgent, since multer only processes multipart/form-data.

The SuperAgent documentation for multipart requests shows the way you do it is to repeat the .field() method:

 request
   .post('/upload')
   .field('user[name]', 'Tobi')
   .field('user[email]', 'tobi@learnboost.com')
   .attach('image', 'path/to/tobi.png')
   .end(callback);
Nocturno
  • 9,579
  • 5
  • 31
  • 39
  • So there is no other way than repeat the .field()? And yes, im using SuperAgent, but since im uploading a file, i thought multer was responsible for that. – Daniel Storch Dec 10 '15 at 15:15
  • The docs also show a `.send()` method that accepts an object, but I didn't see anything about using the `.send()` method with multipart/form-data. You could experiment with `.send()` and perhaps use `.set('Content-Type', 'multipart/form-data')`, but I don't know if it's designed to work that way. Give it a try. – Nocturno Dec 10 '15 at 15:19
  • ok, i will have to repeat the .field(). But since you are here, im trying to download that uploaded file and save it. The user should see it in the browser in the download section. I can't find any example for that.. i think im searching the wrong terms. Do you have any idea? – Daniel Storch Dec 10 '15 at 15:24
  • There's a very good example at the [multer docs](https://github.com/expressjs/multer) that shows how to get started doing this. – Nocturno Dec 10 '15 at 15:33
  • To download the file i just uploaded? I can't find the example for that. Could you post the direct link? thanks – Daniel Storch Dec 10 '15 at 15:35
  • To the users machine. Like this, User ->Uploades image, After upload is finished, User->Downloads same image – Daniel Storch Dec 10 '15 at 15:46
  • Download it where? To the server or let the user download it after they upload it? If you're trying to store it on the server, then the multer docs show how to do that. If your'e trying to let the user download it, then Express lets you do that with [res.download()](http://expressjs.com/en/4x/api.html#res.download). – Nocturno Dec 10 '15 at 15:48