Multer 上传文件数组失败
Multer uploading array of files fail
我有一组文件对象被发送到服务器。
"files[0] = (file object), files[1]= ... "
Multer 无法识别其字段名称,并且 "request.files" 为空,但“request.body 包含文件数组。
据我从您的描述中了解到,您正在使用 upload.array 方法从同一输入字段接受多个文件。确保按照文档中的说明为此方法指定字段名:
.array(fieldname[, maxCount])
Accept an array of files, all with the name fieldname. Optionally error >out if more than maxCount files are uploaded. The array of files will be stored in req.files.
这意味着,如果您有这样的输入:
<input type="file" name="foobar" multiple>
然后在服务器端使用:
var upload = require('multer')({ dest:'uploads/' });
upload.array('foobar');
我的问题是我将文件数组附加到我作为 fd.append(files, fileList)
上传的 FD,我应该做的是一个 for 循环,将文件数组中的每个文件对象附加到 FD文件的字段名称。
谢谢大家的回答。
我假设你可以获取文件对象数组,如果你打印文件数组,console.log(files) => [File, File, File, .... etc]。然后,
获取您的文件和字段,创建 FromData 对象并附加您需要发送到服务器的所有内容,如下所示。确保将 'Content-Type' 设为未定义,您的浏览器将为您分配边界,否则后端将尝试拒绝请求。
this.uploadFileToUrl = function(file, title, text, uploadUrl){
var payload = new FormData();
payload.append("title", title);
payload.append('text', text);
for(var x = 0; x<file.length; x++) {
payload.append('myfile', file[x]);
}
console.log(payload);
return $http({
url: uploadUrl,
method: 'POST',
data: payload,
//assign content-type as undefined, the browser
//will assign the correct boundary for us
headers: { 'Content-Type': undefined},
//prevents serializing payload. don't do it.
transformRequest: angular.identity
});
}
我有一组文件对象被发送到服务器。 "files[0] = (file object), files[1]= ... " Multer 无法识别其字段名称,并且 "request.files" 为空,但“request.body 包含文件数组。
据我从您的描述中了解到,您正在使用 upload.array 方法从同一输入字段接受多个文件。确保按照文档中的说明为此方法指定字段名:
.array(fieldname[, maxCount])
Accept an array of files, all with the name fieldname. Optionally error >out if more than maxCount files are uploaded. The array of files will be stored in req.files.
这意味着,如果您有这样的输入:
<input type="file" name="foobar" multiple>
然后在服务器端使用:
var upload = require('multer')({ dest:'uploads/' });
upload.array('foobar');
我的问题是我将文件数组附加到我作为 fd.append(files, fileList)
上传的 FD,我应该做的是一个 for 循环,将文件数组中的每个文件对象附加到 FD文件的字段名称。
谢谢大家的回答。
我假设你可以获取文件对象数组,如果你打印文件数组,console.log(files) => [File, File, File, .... etc]。然后, 获取您的文件和字段,创建 FromData 对象并附加您需要发送到服务器的所有内容,如下所示。确保将 'Content-Type' 设为未定义,您的浏览器将为您分配边界,否则后端将尝试拒绝请求。
this.uploadFileToUrl = function(file, title, text, uploadUrl){
var payload = new FormData();
payload.append("title", title);
payload.append('text', text);
for(var x = 0; x<file.length; x++) {
payload.append('myfile', file[x]);
}
console.log(payload);
return $http({
url: uploadUrl,
method: 'POST',
data: payload,
//assign content-type as undefined, the browser
//will assign the correct boundary for us
headers: { 'Content-Type': undefined},
//prevents serializing payload. don't do it.
transformRequest: angular.identity
});
}