Storing data stream from POST request in GridFS, express, mongoDB, node.js

后端 未结 5 1725
予麋鹿
予麋鹿 2020-12-01 05:53

I am trying to figure out how I can post an image directly to GridFS without storing it anywhere on the server as a temporary file first.

I am using Postman (chrome

5条回答
  •  日久生厌
    2020-12-01 06:17

    while @robertklep's answer is correct, I would like to add something to his answer. This code shows how you can send back the stored file's metadata.

    app.post('/picture', function(req, res) {
      req.pipe(gfs.createWriteStream({
        filename: 'test'
      }).on('close', function(savedFile){
        console.log('file saved', savedFile);
        return res.json({file: savedFile});
      }));
    })  
    

提交回复
热议问题