Ask coding questions

← Back to all posts
What is the correct way to store files in database
Kapilchn7

I'm working on mevn app, where I'm confused with storing the user files in database. I'm using mongodb for storage and multer to get files as buffer.

Is it compulsory to convert buffer into file(say .png) and then store it on database or we should store buffer directly?

If we convert buffer into file, then we need to delete it from server's memory. For small application we can can convert it into file , but for large application this task will take a lot of time and memory. Also, memory leak might be an issue.

Also, is there any common way to compress image and deliver it to client side (like facebook does).

Thanks in advance

Voters
MatthewWesolows
DynamicSquid
Kapilchn7
Comments
hotnewtop
DynamicSquid

I'm not good with images, but I think the best way to do it is to convert the image into a byte array (or whatever type of buffer you're using), and store that in the database.

I found this answer listing some image manipulation libraries you could use

Hope that helps :)