【问题标题】:cloudinary file too largecloudinary 文件太大
【发布时间】:2022-05-10 23:39:58
【问题描述】:

我正在使用 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. '}))
})

【问题讨论】:

    标签: node.js cloudinary


    【解决方案1】:

    看起来请求正文大于 100 MB,这是 Cloudinary 对上传请求的限制。任何大于此限制的请求都会收到您看到的错误。 要上传大于 100MB 的文件,您必须分块发送请求(请参阅文档 here)。

    您应该使用upload_large 方法而不是使用upload 方法,因为它会拆分文件并自动为您分段上传。 见-https://github.com/cloudinary/cloudinary_npm/blob/4b0bbccc9bc7c9340b59536e7c73e57c55da2e6f/lib/uploader.js#L54

    【讨论】:

      【解决方案2】:

      我意识到这是一个老问题,但我认为这可以帮助像我这样遇到此问题的人。当我收到这个错误时,我发现它不是来自 Cloudinary,而是来自 Express,它会阻止特定大小的请求主体,除非这个限制被明确设置得更高。您应该(如果您使用 Express)能够以这种方式修复它。

      我希望这可以帮助别人!

      app.use(express.json({
        limit: '50mb'
      }));
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-11-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-06-22
        相关资源
        最近更新 更多