【问题标题】:Multer file is not removed if upload is canceled如果取消上传,不会删除 Multer 文件
【发布时间】:2020-09-14 08:28:20
【问题描述】:

Multer中是否有任何功能可以在请求被取消时从服务器中删除文件的上传部分

【问题讨论】:

    标签: node.js storage multer


    【解决方案1】:

    长期以来,这一直是 multer 中的一个错误。请在 github 上查看此问题:https://github.com/expressjs/multer/issues/259,对话中列出了各种解决方法。

    在我们的项目中,我们目前正在使用 in this comment 提到的 fork "kyleerik/multer#kyleerik-patch-1",到目前为止它运行良好。

    【讨论】:

      【解决方案2】:

      这是我的方法对我有用

      
      filename: (req, file, cb) => {
          const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1e9);
          const fileName =
            file.fieldname + '-' + uniqueSuffix + path.extname(file.originalname);
          cb(null, fileName);    
          req.on('aborted', () => {
            const fullFilePath = path.join('uploads', 'videos', fileName);
            file.stream.on('end', () => {
              fs.unlink(fullFilePath, (err) => {
                console.log(fullFilePath);
                if (err) {
                  throw err;
                }
              });
            });
            file.stream.emit('end');
          })
        }
      
      

      【讨论】:

        猜你喜欢
        • 2018-02-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-12
        相关资源
        最近更新 更多