From binary to string on client



How do you convert binary data to string on the client? I have tried all the EJSON* functions with no luck.

I am trying to save images as binary in mongo. First I remove the part with “data:image/jpeg;base64,” then I save the rest with EJSON.newBinary(data). I figure - either I’m doing something wrong or the client can’t convert binary. It works if I don’t strip out the “data:image…” part but then it gets stored as a string which is unnecessarily bigger.

Send it as a blob, no need to convert it to a string.

H! @nlammertyn

I don’t get it. Send it to/from where? I’m trying to display an image.

Well where are you getting your image data from? (canvas, filereader, etc.)

I’d just send the data as a blob from the client to the server, then you can convert, save or whatever you want to do with it on the server.

This here is probably what you need:


Yes. From canvas. The toDataUrl.

I’m downsizing, cropping and properly orienting the image on the client. I figure I could save some bandwidth that way (images from smart phones are huge). But now I’m stuck at how to display them again after making them binary.

Hi @rhywden

ImageData is no go for IE it seems. Does it really take all this? Why do meteor have a way to convert a string to binary and then no way of converting it back? This sounds crazy so I guess I’m just missing something here?

Btw. Appreciate your input.

Use .toBlob() instead and just save that server-side. Again you don’t need to make it a string.

To show it again you transmit it from the server back to the client and make a url for the blob with URL.createObjectURL(). Then just feed that as the src for either an img tag or a new Image() object.

Alright. I’ll try that.


.toBlob() has no support for safari desktop, safari mobile nor Android. Bummer.