收到错误:试图上传多个文件时,意外现场多个、意外、错误、现场

2023-09-14 00:27:33 作者:一懒无娱

如果我在multer的自述使用的例子,我可以上传单个文件没有问题。然而,当我使用同一个样品,我不能为多个文件这样做。我试着用常规

错误:意外现场    在makeError(/Users/mroker/node_projects/JWT_Auth_Basic/node_modules/multer/lib/make-error.js:12:13)    在wrappedFileFilter(/Users/mroker/node_projects/JWT_Auth_Basic/node_modules/multer/index.js:39:19)    在打杂。 (/Users/mroker/node_projects/JWT_Auth_Basic/node_modules/multer/lib/make-middleware.js:112:7)    在emitMany(events.js:108:13)    在Busboy.emit(events.js:182:7)

HTML

 < D​​IV CLASS =按钮BTN BTN-危险NGF选NG模型=文件NAME =文件NGF海报=真正的>选择< / DIV> 

提交

APP.JS

 如果($ scope.files){    的console.log('上传多个作品');    的console.log($ scope.files);    $ scope.uploadFiles($ scope.files);  }};//多个文件$ scope.uploadFiles =功能(文件){  如果(文件和放大器;&安培; files.length){    对于(VAR I = 0; I< files.length;我++){      Upload.upload({        网址:'API /管理/照片,        数据:{文件:文件的[I]}      }),然后(功能(RESP){        的console.log('成功'+ resp.config.data.file.name);        $ scope.myPic = resp.config.data.file.name;      },      功能(RESP){        的console.log('错误状态:'+ resp.status);      },      功能(EVT){        VAR progressPercentage = parseInt函数(100.0 * evt.loaded / evt.total);         的console.log('进步:'+ progressPercentage +'%'+ evt.config.data.file.name);      })      }    }  }; 
文字办公 Word在试图打开文件时遇到错误的解决方法

节点JS

  VAR存储= multer.diskStorage({  目的地:功能(REQ,文件,CB){    CB(NULL,公共/ IMG')  },  文件名:功能(REQ,文件,CB){    // CB(NULL,file.originalname +' - '+ Date.now())    CB(NULL,file.originalname)  }});VAR上传= multer({存储:存储});apiRoutes.post('/管理/照片,upload.array('文件'),功能(REQ,RES){//保存到数据库变种照片=新照片({    网址:req.file.originalname,    名称:req.file.filename});photo.save(函数(ERR,文档){    如果(ERR)抛出走错了路。});的console.log(req.files);的console.log(req.file.originalname);//console.log(req.file.originalname);res.json(req.files);}); 

最后,当我使用了错误与app.use检查我收到以下错误节点

  {[错误:意外现场]code:'LIMIT_UNEXPECTED_FILE',  现场:文件[0]',  storageErrors:[]} 

解决方案

我不得不改变

 数据:{文件:文件}至数据:{文件:文件的[I} 

这解决了它。

If I use the example in multer's readme, I can upload a single file without problems. However, when I use the same sample I cannot do so for multiple files. I tried using the regular

Error: Unexpected field at makeError (/Users/mroker/node_projects/JWT_Auth_Basic/node_modules/multer/lib/make-error.js:12:13) at wrappedFileFilter (/Users/mroker/node_projects/JWT_Auth_Basic/node_modules/multer/index.js:39:19) at Busboy. (/Users/mroker/node_projects/JWT_Auth_Basic/node_modules/multer/lib/make-middleware.js:112:7) at emitMany (events.js:108:13) at Busboy.emit (events.js:182:7)

HTML

<div class="button btn btn-danger" ngf-select ng-model="files" name="files" ngf-multiple="true">Select</div>

submit

APP.JS

if($scope.files){
    console.log('upload multiple works');
    console.log($scope.files);
    $scope.uploadFiles($scope.files);
  }
};

//FOR MULTIPLE FILES
$scope.uploadFiles = function (files) {
  if (files && files.length) {
    for (var i = 0; i < files.length; i++) {
      Upload.upload({
        url: 'api/admin/photos',
        data: {file: files[i]}
      }).then(function(resp){
        console.log('Successfully ' + resp.config.data.file.name);
        $scope.myPic = resp.config.data.file.name;
      },
      function(resp){
        console.log('Error status: ' + resp.status);
      },
      function(evt){
        var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
         console.log('progress: ' + progressPercentage + '% ' + evt.config.data.file.name);
      })
      }
    }

  };

NODE JS

   var storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, 'public/img')
  },
  filename: function (req, file, cb) {
    //cb(null, file.originalname + '-' + Date.now())
    cb(null, file.originalname )
  }
});

var upload = multer({ storage: storage });

apiRoutes.post('/admin/photos',upload.array('files'),function(req,res){

//save to database
var photo = new Photos({
    url: req.file.originalname,
    name: req.file.filename
});

photo.save(function(err,docs){
    if(err) throw err;

});


console.log(req.files);
console.log(req.file.originalname);
//console.log(req.file.originalname);
res.json(req.files);

});

Lastly when I use an error checking with app.use I get the following node error

{ [Error: Unexpected field]


code: 'LIMIT_UNEXPECTED_FILE',
  field: 'file[0]',
  storageErrors: [] }

解决方案

I had to change

data: {file: files}

to

data: {file: files[i}

this solved it.