多个文件上传不起作用

科迪

我正在尝试使用此角度包https://github.com/danialfarid/angular-file-upload上传多个文件以及一些表单数据这是我的代码:

var files = ... // get the files (this is working, don't worry)

return $upload.upload({
    url: '/some_resource',
    type: 'POST',
    headers: { 'Content-Type': 'multipart/form-data' },
    data: { myObj: JSON.stringify(myObj) },
    file: files
});

如果中只有一个文件files,则可以正确上载-但是,如果有多个文件,则不会上载任何内容。在文档中,它说:

上载多个文件:仅对于HTML5 FormData浏览器(不是IE8-9),如果将文件数组传递到文件选项,它将在一个请求中一起上载所有文件。

我不确定自己是否做错了任何事情(我使用的是Chrome)。

我的下一个猜测是问题出在后端(我正在使用Express.js)。由于请求是'multipart/form-data',因此我通过multerhttps://github.com/expressjs/multer运行它,如下所示:

app.post('/some_resource', multer({ dest: '../tmpUploads' }), function(req, res) {
    console.log(req.files); // <- prints {} when uploading multiple files
});

就像我说的那样,当files仅包含一个文件时,我的设置可以完美地工作任何帮助将不胜感激!

科迪

这就是我的工作方式。

var files = [...] // array of the file objects
var fileNames = [...]
// a name in [fileNames] shares the same index as the file it names in [files]

return $upload.upload({
    url: '/some_resource',
    type: 'POST',
    headers: { 'Content-Type': 'multipart/form-data' },
    data: { myObj: JSON.stringify(myObj) },
    file: files,
    fileFormDataName: fileNames
});

我的node.js后端:

app.post('/some_resource', multer({ dest: '../tmpUploads' }), function(req, res) {
    // req.files is an object, where, in terms of the frontend
    // and backend variables, req.files[fileNames[i]] = files[i]
});

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章