gpt4 book ai didi

javascript - "path"参数必须是字符串类型或 Buffer 或 URL cloudinary 和 nodejs 的实例

转载 作者:行者123 更新时间:2023-12-05 04:51:38 28 4
gpt4 key购买 nike

我正在尝试创建一种客户端可以将图像从前端上传到后端服务器然后将图像存储到 cloudinary 的方法,但我最终得到了这个错误:

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

这是我的后端代码库:

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",
});

这是我从控制台得到的

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' }

我该如何解决这个问题?

最佳答案

我有同样的错误。在这里你如何解决它:

npm install datauri

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

由于您使用 multer 解析表单并将其存储在内存存储中:

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

file64 是一个 DataUri 对象。我们不能直接传递它

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

result 对象具有“secure_url”属性,这是文件的安全 https 链接。

关于javascript - "path"参数必须是字符串类型或 Buffer 或 URL cloudinary 和 nodejs 的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66831285/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com