简体   繁体   中英

Image Upload without Multer in Node.js

How can I upload and store images in MongoDB without using Multer/any other middleware? Is this possible?

Definitely possible.

Instead of multer use busboy . And for file storing in mongoDB database, check GridFS

You can use request data stream or pipe it to some other stream something like fs.createWriteStream , here is code example:

request.on('data', (data: any) => {
  console.log(data);
});

Hope this is what you need.

Yes, you can implement it in your code. Middleware such as Multer implements this functionality. So you can do it too. Multer implement over busboy you can check their implementation in their repositories - multer and busboy . Source code is good way for study.
Or see how pipe data from request and you can receive it in your middleware.

req.on('data', data => {
   // ... do your stuff here
});

Install npm install --save express-fileupload

In the app.js/server.js file do this

const upload = require('express-fileupload');
// Then you can set a middleware for express-fileupload
app.use(upload());

You can see the full example of how to do this here .

After doing this you can save the location(url) of your file in mongoDB for referencing it later in a web page.

To know how to access the properties of the file you are uploading eg: fileName etc... see here . Good luck

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM