0
試圖使用multiparty
上載服務器中的多個文件。在服務器端獲取兩個文件,但只寫入單個文件正在上傳。當我調試代碼時,我發現在文件讀取發生之前,控制器正在返回到for循環的開始處。因此,對象中的最新文件僅處理上傳。爲什麼發生這種情況?以下是相同的代碼。沒有上傳到node.js服務器的多個文件
form.parse(req, function(err, fields, files){
var fileArry=files.uploadFiles;
if(fileArry.length == 0){
console.log(" No file found to upload !!!");
res.send('No files found to upload.');
return;
}
for(var i=0; i<fileArry.length ; i++)
{
newPath='./uploads/';
singleFile=fileArry[i];
console.log("::::::::::::::::::::: This is the single file and it path ::::::::::::::::::::::::");
console.log(singleFile);
newPath+=singleFile.originalFilename;
console.log("::::::::::::::::::::: New file path is :"+newPath)
console.log("::::::::::::::::::::: Going inside file :::::::::::::::::::::::::::::::::::::::::::");
fs.readFile(singleFile.path, (err, data) => {
fs.writeFile(newPath, data, (err) => {
console.log("Files uploaded "+newPath);
});
});
}
res.send("File uploaded to: " + newPath);
});
如何克服這一點,並上傳多個文件在同一時間?