无法将图片上传到cloudinary

问题描述 投票:0回答:3

你能告诉我,当我尝试将图像上传到cloudinary时,我做错了什么吗?

app.js 我不需要将图像存储在服务器上,所以我将其存储在内存中。


    var cloudinary = require('cloudinary');
    cloudinary.config({ 
    cloud_name: 'hidden', 
    api_key: 'hidden', 
    api_secret: 'hidden' 
    });
    var multer  = require('multer');
    var storage = multer.memoryStorage()
    var upload = multer({ storage: storage })

场地形式(玉/哈巴狗)


    form(action="/?_csrf="+csrfToken method="post" enctype='multipart/form-data')
    input(type="file" name="avatar")
    input(type="submit" value="upload")

应用程序帖子

   app.post('/', upload.single('avatar'), function(req, res, next){ 
  
     console.log('Should be undefined:', req.file.path);  //yes
      console.log('Should be the buffer:', req.file.buffer); //yes
      cloudinary.uploader.upload(req.file.path, function(result) {           console.log(result) });
    });

我收到错误


    { error: { message: 'Missing required parameter - file', http_code: 400 } }

node.js express cloudinary multer
3个回答
2
投票
i find out how (just use Datauri):

    var dUri = new Datauri();
    dUri.format(path.extname('TEST').toString(), req.file.buffer);

    cloudinary.uploader.upload(dUri.content, function (err, result) {
  if (err) {
    console.log(err);
  } else {
    console.log(result);
  }
});


0
投票

我无法直接从表单上传到服务器,但我使用了一个技巧,首先将文件存储在磁盘上,然后尝试上传我的文件。 我使用 heroku 托管,这意味着我的文件将在 30 分钟后被删除。这意味着我不会有任何存储问题。

//#1 i collect data into storage ./image/filename

await file.mv('./image/' + filename, async (err) => {
        if (err) {
            console.log("server'/upload' : faild to upload error =>" + err)
            res.send('Save files => error : ' + err)

        }
        else {
            try {
                const client = await pool.connect()
                //await client.query(`INSERT INTO test_table(id, name) VALUES(${1},'${"test"}')`)
                const result = await client.query(`INSERT into post(musicname,artistname,price, music, picture)
                VALUES ('${textName}','${textArtist}','${textPrice}', '${musicname}','${filename}')`);
                res.send("server'/upload' : inserting new Data is Done.")
                console.log("server'/upload' : inserting new Data is Done.")
                client.release();
              } catch (err) {
                console.error(err);
                res.send("Error " + err);
              }

        }
    })



    await fileMusic.mv('./music/' + musicname, (err) => {
        if (err) {
            console.log(err)
            res.send('save files => error')

        }
    })
//#2 uplaoding collected data into cloudinary
    await cloudinary.v2.uploader.upload('./image/' + filename, {public_id: `${filename}`}, 
    function(error, result){
        result;
        console.log(result.url, error)
    });

0
投票

从“multer-storage-cloudinary”导入{CloudinaryStorage};

常量存储 = new CloudinaryStorage({ 多云的: 多云的, 参数:(请求,文件)=> { const 文件夹路径 = req.originalUrl === "/api/v1/studentusers" ? “学生门户应用程序/学生个人资料” :“学生门户应用程序/教师简介”; const fileExtension = path.extname(file.originalname).substring(1); const publicId =

${file.fieldname}-${ req.body.student_first_name + req.body.student_middle_name + req.body.student_last_name }-${Date.now()}
;

return {
  folder: folderPath,
  public_id: publicId,
  format: fileExtension,
};

}, });

导出 const multerUpload = multer({ 存储 }); 使用multer cloudinary存储库

© www.soinside.com 2019 - 2024. All rights reserved.