簡體   English   中英

Node JS-如何在使用express-fileupload時限制文件大小

[英]Node JS- How can I limit file size while using express-fileupload

我正在使用express-fileupload ,它工作正常並上傳圖像。 我無法找到限制文件大小的方法,或者寫一個檢查,我確保沒有上傳大小超過1MB的文件。

    app.post('/upload', function(req, res) {
  if (!req.files)
    return res.status(400).send('No files were uploaded.');

  // The name of the input field (i.e. "sampleFile") is used to retrieve the uploaded file
  let sampleFile = req.files.sampleFile;

  // Use the mv() method to place the file somewhere on your server
  sampleFile.mv('/somewhere/on/your/server/filename.jpg', function(err) {
    if (err)
      return res.status(500).send(err);

    res.send('File uploaded!');
  });
});

我試過這樣的東西,實際上是剪掉剩下的圖像

    app.use(fileUpload({
    limits: {
        fileSize: 1000000 //1mb
    },
}));

我可以通過檢查每個文件的文件大小來執行此JavaScript,但是沒有任何內置功能??? 單擊即可上傳多個文件? 對於多個文件,它將通過循環並檢查每個文件大小並排除那些大小超過1mb的文件,並僅上傳那些大小符合要求的文件。 所以我想知道除了編寫我自己的代碼是不是沒有任何內置功能???

在express-fileupload中,我可以看到busboy選項。 你試過嗎? https://github.com/richardgirges/express-fileupload#using-busboy-options

我想在評論中寫這個,但我沒有足夠的聲譽點。 :(

它正在截斷剩余的圖像,因為達到了大小限制,並且在這種情況下您沒有明確設置中間件以中止上載。

因此,嘗試將選項“abortOnLimit”設置為true,如下所示:

 app.use(fileUpload({
    limits: {
        fileSize: 1000000 //1mb
    },
    abortOnLimit: true
 }));

有關更多信息,這是文檔講述使用選項'abortOnLimit'的內容:

如果文件大於大小限制,則返回HTTP 413(如果為true)。 否則,它將為生成的文件結構添加truncate = true。

來源鏈接: https//www.npmjs.com/package/express-fileupload

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM