【问题标题】:Formidable multi-file upload - ENOENT, no such file or directory强大的多文件上传 - ENOENT,没有这样的文件或目录
【发布时间】:2014-10-08 08:03:49
【问题描述】:

我知道有人问过这个问题,但我无法做到这一点让我大吃一惊。我正在尝试使用以下代码将多张图片上传到我的服务器:

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' 通过重命名语句。

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

我很困惑,什么都试过了。我做错了什么?

【问题讨论】:

  • 是关于为什么你得到ENOENT 的问题,或者为什么路径不是其他值。它抱怨的目录是否存在?
  • 问题是我为什么会收到ENOENT。该目录确实存在。我知道这一点是因为没有try/catch 服务器会崩溃,并且因为这发生在强大的清理上传目录之前,文件仍然存在。
  • 是否有任何堆栈跟踪错误?只是错误消息没什么用。
  • 你试过关闭终端以外的所有东西吗?
  • @loganfsmyth 我正在工作,所以目前无法生成堆栈跟踪。

标签: javascript node.js file-upload multipart formidable


【解决方案1】:

您可能会收到此错误,因为目标路径不存在或您没有写入权限。

由于 nodejs 中的错误,您得到的错误具有误导性,请参阅:

考虑添加:

console.log(file.file.endPath);

fs.renameSync 调用之前检查目标路径是否存在并且是否可由您的应用程序写入

【讨论】:

    【解决方案2】:

    你说的形式。因此请注意,仅使用 NodeJS 并不能开箱即用地使用 Formidable。除非您要使用提示模块之类的东西进行输入。如果您使用的是 HTML,则需要 Angular、React 或 Browserify 之类的东西才能使其访问您的界面。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-03
      • 2014-01-12
      • 1970-01-01
      • 2023-03-31
      • 2015-04-14
      • 2013-07-28
      • 2019-02-03
      • 1970-01-01
      相关资源
      最近更新 更多