避免对忙碌的文件上传大小限制进行进一步处理

人气:583 发布:2022-10-16 标签: node.js express upload limit busboy

问题描述

我正在尝试设置服务生的上传限制。没有上载限制,我能够成功上载文件。

但是,如果上载文件大小超出要求,我想要重定向时,我意识到代码是异步的,文件写入或上载无论如何都会发生。

我想做的是,如果限制达到配置值,它应该重定向到页面,而不上传文件。我已尝试使用Javasript Promise,但无济于事。

我的服务员代码是这样的。

var express = require('express');
var router = express.Router();
var inspect = require('util').inspect;
var Busboy = require('busboy');

router.all('/', function(req, res, next) {

if (req.method === 'POST') {
  var busboy = new Busboy({ headers: req.headers, limits: { files: 1, fileSize: 30000000 }  });

  busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
    file.on('limit', function(data) {
      console.log("limit...");
      console.log("ONEEE");
    });
    console.log("TWOOO");
    console.log('File [' + fieldname + ']: filename: ' + filename + ', encoding: ' + encoding + ', mimetype: ' + mimetype);

    file.on('data', function(data) {
      console.log('File [' + fieldname + '] got ' + data.length + ' bytes');
    });
    file.on('end', function() {
      console.log('File [' + fieldname + '] Finished');
    });
  });

  busboy.on('finish', function() {
    console.log('Done parsing form!');
    res.writeHead(303, { Connection: 'close', Location: '/test_upload' });
    res.end();
  });
  req.pipe(busboy);
}
});

module.exports = router;
在这里,我已将文件大小限制指定为30 MB。但是当我上传一个比方说40MB的文件时,我仍然在控制台上看到"Twoo",然后是"Oneee"……这显然是因为这是异步发生的,那么解决方案是什么...?

基本上,如果达到限制,我希望记录"ONEEE"并重定向,从而避免将"Twooo"记录到控制台并避免文件处理。

此外,如果我尝试检查内部的file.on(‘data’...上的(‘FILE’... 我在使用文件管道的文件系统上收到不规律的文件大小上载。

例如,它的工作不一致(当文件显示为354字节或类似字节时,它显示为0字节):

busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
      file.on('data', function(data){
        fstream = fs.createWriteStream("/home/vibs/temp_01.txt");
        file.pipe(fstream);
      });

    });
但是,如果我从内部删除了file.on(‘data’),那么...流已正确写入磁盘,但无法检查文件大小是否已超过...

因此,将文件写入文件系统是正确的。但我无法检查文件大小是否超过允许的限制...

busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
  fstream = fs.createWriteStream("/home/vibs/temp_01.txt");
  file.pipe(fstream);
});
因此,我的问题是如何在不执行上传过程的情况下检查文件大小限制和重定向...File.On(‘data’...失败,因为流在函数内部损坏...和file.on(‘Limit’被异步调用,因此无法避免首先运行文件写入脚本以避免不必要的上载..

推荐答案

这将在文件达到最大大小时停止上载。

var fstream;
req.pipe(req.busboy);

req.busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {

  var path = __dirname + "/../public/";
  var limit_reach = false;
  var limit_reach_err = "File is too large!";

  // Creating a writestream using fs module
  fstream = fs.createWriteStream(path);
  file.pipe(fstream);


   // If the file is larger than the set limit
   // delete partially uploaded file 
   file.on('limit', function(){
     fs.unlink(path, function(){ 
       limit_reach = true;
       res.status(455).send(limit_reach_err);
      });
    });

    // Despite being asynchronous limit_reach 
    // will be seen here as true if it hits max size 
    // as set in file.on.limit because once it hits
    // max size the stream will stop and on.finish
    // will be triggered.
    req.busboy.on('finish', function() {
      if(!limit_reach){
        res.send("Image saved successfully!");
      }
    });
 });

793