Angularjs 多文件上载不工作
我正在尝试使用这个angular软件包上载多个文件以及一些表单数据。这是我的密码:Angularjs 多文件上载不工作,angularjs,file-upload,express,Angularjs,File Upload,Express,我正在尝试使用这个angular软件包上载多个文件以及一些表单数据。这是我的密码: 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:
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
});
如果文件
中只有一个文件,则会正确上载-但如果有多个文件,则不会上载任何内容。文件中说:
上载多个文件:仅适用于HTML5 FormData浏览器(不是IE8-9),如果您将一组文件传递到文件选项,它将在一个请求中上载所有文件
我不太确定我是否做错了什么(我使用的是Chrome)
我的下一个猜测是问题出在后端(我使用的是Express.js)。由于请求是'multipart/formdata'
,因此我正在通过multer
()运行它,如下所示:
app.post('/some_resource', multer({ dest: '../tmpUploads' }), function(req, res) {
console.log(req.files); // <- prints {} when uploading multiple files
});
app.post('/some_resource',multer({dest:'../tmpUploads'}),函数(req,res){
console.log(req.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
});
My 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]
});
首先,我会尝试使用Fiddler来检查问题是在客户端还是在服务器上。其次,尝试添加文件名:fileName
或fileFormDataName
属性。我曾经尝试过这个模块,但由于我无法放弃对IE8/9的支持,现在我正在使用它,它非常有效。添加fileFormDataName
属性完全有效!谢谢!@Cody,你能告诉我们你是如何工作的吗?我也面临同样的问题,并尝试过使用fileFormDataName
,但迄今为止效率不高……非常感谢你提供的任何帮助:)@PA.Buisson刚刚发布了我的解决方案作为答案!