my code app.js
it is to upload some of imges I am getting the "TypeError: Cannot read property 'length' of undefined?
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();
}
});
ERROR: { if(arrayOffiles.length > 0){ TypeError: Cannot read property 'length' of undefined}
how can i solve it please in node.js
Use this
var arrayOffiles= files || [];
Instead
var arrayOffiles= files[""];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.