我使用cloudinary来上传和存储图片。当我尝试上传一个简单的JPG文件时,它给出了以下错误:
"PayloadTooLargeError: request entity too large"
{
message: 'request entity too large',
expected: 1127957,
length: 1127957,
limit: 102400,
type: 'entity.too.large'
}以下是我的后端代码:
cloudinary.config({
cloud_name: process.env.CLOUD_NAME,
api_key: process.env.CLOUD_API_KEY,
api_secret: process.env.CLOUD_API_SECRET
})
exports.uploadImage = catchAsync(async(req, res, next) => {
cloudinary.uploader.upload(req.body.img)
.then(async res => {
await User.updateOne({
_id: req.user._id
},{
$push: {
images: {
imgId: res.public_id,
imgVersion: res.version
}
}
})
})
.then(() => res.status(200).json({ msg: 'image uploaded. '}))
})发布于 2020-09-30 20:34:39
看起来请求正文大于100MB,这是Cloudinary对上传请求的限制。任何大于此限制的请求都会收到您看到的错误。要上传大于100MB的文件,您必须以块的形式发送请求(参见文档here)。
您应该使用upload_large方法,而不是使用upload方法,因为它会自动拆分文件并将其分块上传。请参见- https://github.com/cloudinary/cloudinary_npm/blob/4b0bbccc9bc7c9340b59536e7c73e57c55da2e6f/lib/uploader.js#L54
https://stackoverflow.com/questions/64136483
复制相似问题