Multer not accepting files in array format gives 'Unexpected File Error'

若如初见. 提交于 2019-11-30 09:07:01

The reason for the error is that multer currently does not support the array syntax that ng-file-upload uses by default which is files[0], files[1], files[2], etc. multer is expecting a series of files with the same field name.

The easiest solution is to set ng-file-upload's arrayKey option like so to avoid appending the [index] part:

Upload.upload({
  url: '/api/data/addtweet',
  arrayKey: '', // default is '[i]'
  data: {
    files: files
  }
})
Prasad19sara

If someone is facing a similar issue when uploading a custom form data object, you can use this approach. In here I am not using ngFileUpload.

Client Side code

var fd = new FormData();

for( var i =0; i< files.length ; i++ ){
    fd.append('fileItems' , files[i] );
}

fd.append('_id', params._id );              
fd.append('user', params.user );              

return $http.post( ROOT_URL + '/uploadFiles/', fd, {
    transformRequest: angular.identity,
    headers: {'Content-Type': undefined }
});

Express Router

var multer  = require("multer");
var upload = multer({ dest: "uploads/" });

app.post('/api/uploadFiles', upload.array('fileItems'), handlers.files.uploadFiles);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!