How do I upload multiple images using multer in node js?

Debashish Dwivedi

I am making API for multiple image uploading in node js. So can you please help me out, how to upload only images and not another file

var Express = require('express');
var multer = require('multer');
var bodyParser = require('body-parser');
var app = Express();
app.use(bodyParser.json());
var Storage = multer.diskStorage({
    destination: function (req, file, callback) {
        callback(null, "./Images");
    },
    filename: function (req, file, callback) {
        callback(null, file.fieldname + "_" + Date.now() + "_" + file.originalname);
    }
});

var upload = multer({ storage: Storage }).array("imgUploader", 3); //Field name and max count

app.get("/", function (req, res) {
    res.sendFile(__dirname + "/index.html");
});

app.post("/api/Upload", function (req, res) {
    upload(req, res, function (err) {

        if (err) {
            return res.end("Something went wrong!");
        }
        return res.end("File uploaded sucessfully!.");
    });
});
FuriousD

You can add a fileFilter function to your multer options. So with a bit of refactoring:

var options = {
  storage: multer.diskStorage({
      destination: function (req, file, callback) {
          callback(null, "./Images");
      },
      filename: function (req, file, callback) {
          callback(null, file.fieldname + "_" + Date.now() + "_" + file.originalname);
      }
  }),
  fileFilter: function (req, file, callback) {
    const isPhoto = file.mimetype.indexOf("image/") === 0
    if (isPhoto) {
      callback(null, true) // true if valid
    } else {
      callback({ message: 'An optional error message'}, false) // false if invalid
    }
  }
}

And then use these options:

var upload = multer(options).array("imgUploader", 3);

See: https://github.com/expressjs/multer#filefilter

accept="image/*" on your input will only provide client side validation, and can be removed with the dev console.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how do i upload multiple images using Multiparty Entity in Android

From Dev

How to specify destination in multer to upload images to remote server folder in node js?

From Dev

How to upload multiple images at a time in s3 bucket using Node JS (Express)

From Dev

node.js to upload image using multer shows undefined

From Dev

How do I upload multiple images in my WebView?

From Dev

How do I upload multiple images in my WebView?

From Dev

How can i upload multiple files in node.js?

From Dev

node multer AWS single file multiple upload

From Dev

How do I send a file from postman to node.js with multer

From Dev

How to upload multiple images using codeigniter?

From Dev

How do I update multiple rows using an array in mysql using node js

From Dev

How Do I overlay MULTIPLE Images in HTML, CSS, And JS?

From Dev

Multiple image upload using Node.js

From Dev

How do I to show images before upload?

From Dev

How do I get iPads to upload images?

From Dev

can not multiple upload using dropzone and multer

From Dev

How do I allow MULTIPLE images to be moved using touch in SpriteKit?

From Dev

Upload image with Java MultipartEntity to Node.js server using express and multer

From Dev

Retaining image file name and extension after upload in node.js (express) using multer

From Dev

How do I upload multiple images and text file in a single form with the different button in Codeigniter?

From Dev

How do I "wrap" a response with multiple results in Node.JS

From Dev

How do I get the body of a request from npm's multer if I don't upload a file?

From Dev

Upload multiple images using AFNetworking

From Dev

Using Multer for uploading files in node js

From Dev

How do I upload multiple files with Protractor?

From Dev

How do I serialize the form's file to upload so that it can be stored in a mongo db, using Backbone.js, jQuery and Node.js?

From Dev

How do I serialize the form's file to upload so that it can be stored in a mongo db, using Backbone.js, jQuery and Node.js?

From Dev

Node.js connection reset on file upload with multer

From Dev

Node js request entity too large with multer upload

Related Related

  1. 1

    how do i upload multiple images using Multiparty Entity in Android

  2. 2

    How to specify destination in multer to upload images to remote server folder in node js?

  3. 3

    How to upload multiple images at a time in s3 bucket using Node JS (Express)

  4. 4

    node.js to upload image using multer shows undefined

  5. 5

    How do I upload multiple images in my WebView?

  6. 6

    How do I upload multiple images in my WebView?

  7. 7

    How can i upload multiple files in node.js?

  8. 8

    node multer AWS single file multiple upload

  9. 9

    How do I send a file from postman to node.js with multer

  10. 10

    How to upload multiple images using codeigniter?

  11. 11

    How do I update multiple rows using an array in mysql using node js

  12. 12

    How Do I overlay MULTIPLE Images in HTML, CSS, And JS?

  13. 13

    Multiple image upload using Node.js

  14. 14

    How do I to show images before upload?

  15. 15

    How do I get iPads to upload images?

  16. 16

    can not multiple upload using dropzone and multer

  17. 17

    How do I allow MULTIPLE images to be moved using touch in SpriteKit?

  18. 18

    Upload image with Java MultipartEntity to Node.js server using express and multer

  19. 19

    Retaining image file name and extension after upload in node.js (express) using multer

  20. 20

    How do I upload multiple images and text file in a single form with the different button in Codeigniter?

  21. 21

    How do I "wrap" a response with multiple results in Node.JS

  22. 22

    How do I get the body of a request from npm's multer if I don't upload a file?

  23. 23

    Upload multiple images using AFNetworking

  24. 24

    Using Multer for uploading files in node js

  25. 25

    How do I upload multiple files with Protractor?

  26. 26

    How do I serialize the form's file to upload so that it can be stored in a mongo db, using Backbone.js, jQuery and Node.js?

  27. 27

    How do I serialize the form's file to upload so that it can be stored in a mongo db, using Backbone.js, jQuery and Node.js?

  28. 28

    Node.js connection reset on file upload with multer

  29. 29

    Node js request entity too large with multer upload

HotTag

Archive