强大的多file upload – ENOENT,没有这样的文件或目录

我知道这个问题已经被问到,但是我的思想已经被我无法得到这个工作所吹。 我正在尝试使用以下代码将多个图像上传到我的服务器:

var formidable = require('formidable'); var fs = require('fs'); 

 router.post('/add_images/:showcase_id', function(req, res){ if(!admin(req, res)) return; var form = new formidable.IncomingForm(), files = []; form.uploadDir = global.__project_dirname+"/tmp"; form.on('file', function(field, file) { console.log(file); file.image_id = global.s4()+global.s4(); file.endPath = "/img/"+file.image_id+"."+file.type.replace("image/",""); files.push({field:field, file:file}); }); form.on('end', function() { console.log('done'); console.log(files); db.get("SOME SQL", function(err, image_number){ if(err){ console.log(err); } var db_index = 0; if(image_number) db_index = image_number.image_order; files.forEach(function(file, index){ try{ //this line opens the image in my computer (testing) require("sys").exec("display " + file.file.path); console.log(file.file.path); fs.renameSync(file.file.path, file.file.endPath); }catch (e){ console.log(e); } db.run( "SOME MORE SQL"')", function(err){ if(index == files.length) res.redirect("/admin/gallery"+req.params.showcase_id); }); }); }); }); form.parse(req); }); 

通过系统调用打开图像的行很好,但是我继续得到: Error: ENOENT, no such file or directory '/home/[username]/[project name]/tmp/285ef5276581cb3b8ea950a043c6ed51'重命名语句Error: ENOENT, no such file or directory '/home/[username]/[project name]/tmp/285ef5276581cb3b8ea950a043c6ed51'

file.file.path的值是: /home/[username]/[project name]/tmp/285ef5276581cb3b8ea950a043c6ed51

我很困惑,并尝试了一切。 我究竟做错了什么?

可能你得到这个错误,因为目标path不存在,或者你没有写权限。

由于nodejs中的错误,你得到的错误是误导,请参阅:

考虑添加:

 console.log(file.file.endPath); 

fs.renameSync调用之前,检查目标path是否存在,并且可以被应用程序写入