'cloudinary file too large
I'm using cloudinary for image upload and storage. When I try to upload a simple JPG file, it gives me the following error:
"PayloadTooLargeError: request entity too large"
{
message: 'request entity too large',
expected: 1127957,
length: 1127957,
limit: 102400,
type: 'entity.too.large'
}
following is my backend code:
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. '}))
})
Solution 1:[1]
Looks like the request body is larger than 100 MB which is Cloudinary's limit for upload requests. Any request that is larger than this limit would receive the error you're seeing. To upload files larger than 100MB you'll have to send the request in chunks (see docs here).
Instead of using the upload
method, you should use the upload_large
one as it splits the file and uploads it in parts automatically for you.
See - https://github.com/cloudinary/cloudinary_npm/blob/4b0bbccc9bc7c9340b59536e7c73e57c55da2e6f/lib/uploader.js#L54
Solution 2:[2]
I realize this is an old issue, but I thought this could help people like me who ran into this. What I discovered when I got this error was that it was not coming from Cloudinary but rather from Express, which blocks request bodies of a certain size unless this limit is expressly set higher. You should (provided you are using Express), be able to fix it this way.
I hope this can help someone!
app.use(express.json({
limit: '50mb'
}));
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Roee Ben-Ari |
Solution 2 | Jerud |