0

Right now I am checking the file size and blocking any further action.

if (req.file.size > 4000000) {
          return "Image too big";
}

But the file is still downloaded. Sure I can delete it but how can I prevent the file from being downloaded by the server if it's over a certain size without downloading the file?

EDIT: The reason body-parser limit didn't work for me is because I'm using multer.

Solution:

const upload = multer({ dest: "uploads/", limits: { fileSize: 4000000 } });
mitchldtn
  • 423
  • 1
  • 4
  • 18
  • Does this answer your question? [Node.js: how to limit the HTTP request size and upload file size?](https://stackoverflow.com/questions/9049993/node-js-how-to-limit-the-http-request-size-and-upload-file-size) – giraffesyo Jun 15 '21 at 23:04
  • I tried `app.use(express.json({ limit: "3mb" })); app.use(express.urlencoded({ limit: "3mb", extended: false })); app.use(bodyParser.urlencoded({ limit: "3mb", extended: true })); app.use(bodyParser.json({ limit: "3mb" }));` But it didn't prevent the file being downloaded. – mitchldtn Jun 15 '21 at 23:10
  • It should only download first 3MB (up to your limit) of the file then it will stop the request. – giraffesyo Jun 15 '21 at 23:10

0 Answers0