Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

multer gridfs storage

all you need is here! m/

conn.once('open', ()=> {
    //Init stream
    let gfs = Grid(conn.db, mongoose.mongo);
    gfs.collection('uploads');
})
    // creating storage engine
    var storage = new GridFsStorage({
        url: URI,
        file: (req, file) => {
          return new Promise((resolve, reject) => {
            crypto.randomBytes(16, (err, buf) => {
              if (err) {
                return reject(err);
              }
              const filename = buf.toString('hex') + path.extname(file.originalname);
              const fileInfo = {
                filename: filename,
                bucketName: 'uploads'
              };
              resolve(fileInfo);
            });
          });
        }
      });
      const upload = multer({ storage }); //upload is working as a middleware here!
 
PREVIOUS NEXT
Tagged: #multer #gridfs #storage
ADD COMMENT
Topic
Name
3+8 =