【问题标题】:Image save not working in formidable when using 'file' for form.on对 form.on 使用“文件”时,图像保存无法正常工作
【发布时间】:2023-03-28 11:15:01
【问题描述】:

以下是我使用 form.parse 时工作的代码,但它不会进入 form.on('file', ... 让我知道我在这里做错了什么,因为我需要在保存之前更改文件名。

// CREATE
exports.upload = function (req, res) {

    var form = new formidable.IncomingForm();

    form.uploadDir = __dirname + '/upload';
    console.log("--FORM--");

    /*
    form.parse(req, function(err, fields, files) {
      console.log('received upload:\n\n');
      console.log(util.inspect({fields: fields, files: files}));
    });
    */
    form.on('file', function(field, file) {
        console.log("I am in file.");
        //rename the incoming file to the file's name
        //fs.rename(file.path, form.uploadDir + "/" + file.name);
    })


};

仅供参考 - 如果我使用 form.parse 图像会像 0382cb1bd421ee62b27dc431ab03270c 一样被保存,因为我想重命名它,但在尝试通过 form.on('file', ...) 执行此操作时不起作用。

【问题讨论】:

    标签: node.js image file-upload formidable


    【解决方案1】:

    我使用fileBegin 更改名称解决了这个问题,但您需要包含 form.parse(req); 来保存图像。

    工作代码-

    form.on('fileBegin', function(name, file) {
        file.path = form.uploadDir + "/" + file.name;
    })
    
    form.parse(req);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-31
      • 2015-07-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多