我试图创建一个方法,客户端可以从前端上传图像到后端服务器,然后将图像存储到cloudinary,但我最终得到这个错误:
TypeError [ERR_INVALID_ARG_TYPE]:“path”参数必须是字符串类型,或者是Buffer或URL的示例。
下面是我的后端代码库:
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' }
如何解决此问题?
2条答案
按热度按时间at0kjp5o1#
我也犯了同样的错误,下面是你如何解决的:
npm install datauri
因为你用multer解析表单并将其存储在内存中:
file64
是一个DataUri对象,不能直接传递此
result
对象具有“secure_url”属性,这是到文件的安全https链接。a1o7rhls2#
你可能会错过dataUri函数,它将格式化缓冲区以将其作为字符串返回?请参阅参考资料:https://medium.com/@joeokpus/uploading-images-to-cloudinary-using-multer-and-expressjs-f0b9a4e14c54
让我知道这是否有帮助。