简体   繁体   中英

Storing base64 strings to GridFS(mongoDB file storage)

I'm trying to paste an image from the clipboard into an <img> tag. The source of this image is Prnt Scrn command and not a file. This clipboard image would be in base64 format. This base64 string can be inserted into src attribute of <img> tag(once ctrl-v is pressed) using javascript for display purposes. This is accomplishable by using this plugin .

So the <img> tag would be something like this:

<img id="screen_image" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAABVYAAAMACAI......(long string here)"

Although, I could persist this entire string into a mongoDB collection and retrieve it back for displaying that image, my ultimate goal is to persist this image into gridFS . Is there a way if I could interpret base64 as a file and persist it into gridFS ?

I hope I've made it clear. Comments welcome.

UPDATE: I want to maintain a common collection to store images or any file for that matter(I'm already using gridFS to persist file attachments so I do not want to create a new collection to store clipboard images). I have also tried decoding the string using window.atob() but then I don't know how that could be persisted to gridFS

I'm using Mongo for my senior project right now and previously I was storing child (client is a local non profit like world vision) pictures with GridFS but recently I've moved to storing them in the actual child doc in base64. All of the images are around 3mb and base64 converts out to mostly 4-5mb. If you can store them as base64 it makes for a much simpler schema, I think.

I know you said you wanted to use GridFS, but I would only go that route if you have files over 16mb. For what I hope are obvious reasons it's much simpler to just store them as strings in docs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM