我的代码app.js
它是上传一些imges
我得到" TypeError:无法读取属性'长度'未定义?
app.post('/upload', urlencodedParser, function(req, res) {
if (req.url == '/upload') {
var form = new formidable.IncomingForm();
form.uploadDir = "C:/Users/Abdulrahman Afify/myapp/";
form.keepExtensions = true;
form.multiples = true;
form.parse(req, function(err, fields, files) {
if (err) {
res.json({
results: "faild",
data: {},
message: 'Cannot upload Files Error is: ${err}'
});
}
var arrayOffiles = files[""];
if (arrayOffiles.length > 0) {
var fileNames = [];
arrayOffiles.forEach((eachFile) => {
fileNames.push(eachFile.path);
});
res.json({
result: "OK",
Date: fileNames,
numberOfImages: fileNames.length,
message: "upload images Successfully"
});
} else {
res.json({
result: "failed",
Date: [],
numberOfImages: 0,
message: "upload images failed"
});
}
});
} else {
res.writeHead(200, {
'Content-Type': 'text/html'
});
res.write('<form action="fileupload" method="post" enctype="multipart/form-data">');
res.write('<input type="file" name="filetoupload" multiple ><br>');
res.write('<input type="submit">');
res.write('</form>');
return res.end();
}
});
错误:{if(arrayOffiles.length&gt; 0){ TypeError:无法读取属性&#39; length&#39;未定义}
我怎么能在node.js
中解决它答案 0 :(得分:0)
使用此
var arrayOffiles= files || [];
相反
var arrayOffiles= files[""];