【发布时间】:2017-01-01 11:55:47
【问题描述】:
正在尝试使用multiparty 在服务器中上传多个文件。在服务器端获取两个文件,但在写入时只上传单个文件。当我调试代码时,我发现在文件读取发生之前,控件正在返回 for 循环的开头。因此,仅处理对象中的最新文件以供上传。为什么会这样?下面是相同的代码。
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);
});
如何克服这个问题并一次上传多个文件?
【问题讨论】:
标签: javascript node.js file file-upload