admin管理员组

文章数量:1384229

I am trying to create a method that client can upload image from front end to back end server and then store the images to cloudinary but I end up getting this error:

TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string or an instance of Buffer or URL. Received undefined

Here is my backend code base:

const express = require("express");
const router = express.Router();
const { catchErrors } = require("../errors/errorHandlers");
const { body } = require("express-validator");
const multer = require('multer');
const cloudinary = require('cloudinary').v2;
const streamifier = require('streamifier');
const storage = multer.memoryStorage();
const fileUpload = multer({ storage: storage });
const fs = require('fs'); 

cloudinary.config({
  cloud_name: "xxx",
  api_key: "xxxx",
  api_secret: "xxxx",
});

this is what I got from console

Images  {
  file: {
    name: 'me.jpg',
    data: <Buffer ff d8 ff e0 00 10 4a 46 49 46 00 01 01 00 00 f0 00 f0 00 00 ff e1 03 78 45 78 69 66 00 00 4d 4d 00 2a 00 00 00 08 00 09 01 0f 00 02 00 00 00 06 00 00 ... 442191 more bytes>,
    size: 442241,
    encoding: '7bit',
    tempFilePath: '',
    truncated: false,
    mimetype: 'image/jpeg',
    md5: 'a01832d1a390b2bcd7e2b6103d68eaa4',
    mv: [Function: mv]
  }
}

{ message: 'Request Timeout', http_code: 499, name: 'TimeoutError' }

How can I fix this problem?

I am trying to create a method that client can upload image from front end to back end server and then store the images to cloudinary but I end up getting this error:

TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string or an instance of Buffer or URL. Received undefined

Here is my backend code base:

const express = require("express");
const router = express.Router();
const { catchErrors } = require("../errors/errorHandlers");
const { body } = require("express-validator");
const multer = require('multer');
const cloudinary = require('cloudinary').v2;
const streamifier = require('streamifier');
const storage = multer.memoryStorage();
const fileUpload = multer({ storage: storage });
const fs = require('fs'); 

cloudinary.config({
  cloud_name: "xxx",
  api_key: "xxxx",
  api_secret: "xxxx",
});

this is what I got from console

Images  {
  file: {
    name: 'me.jpg',
    data: <Buffer ff d8 ff e0 00 10 4a 46 49 46 00 01 01 00 00 f0 00 f0 00 00 ff e1 03 78 45 78 69 66 00 00 4d 4d 00 2a 00 00 00 08 00 09 01 0f 00 02 00 00 00 06 00 00 ... 442191 more bytes>,
    size: 442241,
    encoding: '7bit',
    tempFilePath: '',
    truncated: false,
    mimetype: 'image/jpeg',
    md5: 'a01832d1a390b2bcd7e2b6103d68eaa4',
    mv: [Function: mv]
  }
}

{ message: 'Request Timeout', http_code: 499, name: 'TimeoutError' }

How can I fix this problem?

Share Improve this question edited Apr 6, 2023 at 20:13 Yilmaz 50k18 gold badges218 silver badges271 bronze badges asked Mar 27, 2021 at 12:22 Nathan NguyenNathan Nguyen 1313 silver badges13 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

I had the same error. here how you solve it:

npm install datauri

const DatauriParser=require("datauri/parser");
const parser = new DatauriParser();

since you parse the form with multer and store it in memory storage:

  console.log("req.file object",req.file)
  const extName = path.extname(req.file.originalname).toString();
  const file64 = parser.format(extName, req.file.buffer);

file64 is a DataUri object. we cannot pass it directly

const result = await Cloudinary.upload(file64.content!);
console.log("result of Cloudinary upload",result")

this result object has "secure_url" property this is the secure https link to the file.

Are you maybe missing the dataUri function that will format the buffer to return it as a string? See resource: https://medium./@joeokpus/uploading-images-to-cloudinary-using-multer-and-expressjs-f0b9a4e14c54

Let me know if this helps or not.

本文标签: