From binary to string on client


#1

Hi!

TL;DR

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.


#2

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


#3

H! @nlammertyn

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


#4

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.


#5

This here is probably what you need:

Specifically:


#6

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.


#7

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.


#8

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.


#9

Alright. I’ll try that.


#10

@nlammertyn

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