Sharing binary data between PHP and NodeJS - without b64?

89 views
Skip to first unread message

Ingwie Phoenix

unread,
Sep 30, 2014, 4:56:31 PM9/30/14
to nod...@googlegroups.com
The title says it rather well.

But to elaborate a little: I have a web service that can collect data from a database, clean it and then send it to a user as a backup. This backup is supposed to contain binary data, like pictures. But how do I parse such with nodejs - and what format to use? There is a NodeJS/browser implementation of BSON, but for PHP you need a compile, which is more or less a downside.

Does anyone have an idea how to do that, without using base64 within a JSON object, or is that the only way to go?

Kind regards, Ingwie.

Aria Stewart

unread,
Sep 30, 2014, 5:04:17 PM9/30/14
to nod...@googlegroups.com
The web is 8-bit clean! Just send it!

In node, that'd be represented as a buffer; if the file is supposed to be saved by the user via a browser, set the content-disposition header to trigger it to download rather than display.

PHP strings are just strings of bytes, not characters, so it can just deal with binary data like node does in a buffer.

Aria

Ingwie Phoenix

unread,
Sep 30, 2014, 5:11:14 PM9/30/14
to nod...@googlegroups.com
Cool, nice to know! :)

Now, what is if I wanted to save a structure like this and send it to my client?

{
"files": {
"file.jpg": <data>,
"name": "My File"
> --
> Job board: http://jobs.nodejs.org/
> New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
> Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> ---
> You received this message because you are subscribed to the Google Groups "nodejs" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
> To post to this group, send email to nod...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/E80B2BE8-91BC-4675-BB03-A9A3EEB82C99%40nbtsc.org.
> For more options, visit https://groups.google.com/d/optout.

Aria Stewart

unread,
Sep 30, 2014, 5:15:48 PM9/30/14
to nod...@googlegroups.com

On Sep 30, 2014, at 5:07 PM, Ingwie Phoenix <ingwi...@googlemail.com> wrote:

> Cool, nice to know! :)
>
> Now, what is if I wanted to save a structure like this and send it to my client?
>
> {
> "files": {
> "file.jpg": <data>,
> "name": "My File"
> }
> }


JSON is UTF-8, so you're at the mercy of the base64 gods there.

How about using HTTP metadata?

res.setHeader('content-disposition', 'attachment; filename=MyFile');

Or if it's custom metadata, res.setHeader('x-my-metadata', 'value');

If you need to send multiple files, perhaps send a zip or tar of the files. Browsers have no support for saving multiple files.

Aria

Ingwie Phoenix

unread,
Sep 30, 2014, 5:22:32 PM9/30/14
to nod...@googlegroups.com
I see, thats one solution - and you just reminded me on this hTTP header i keep forgetting too! Thanks for that by the way.

I have to sen dmultiple files and retain some metadata (title, description). Is there some kind of JSON that also lets me work with binary data - in both languages that is?

- Ingwie
> --
> Job board: http://jobs.nodejs.org/
> New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
> Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> ---
> You received this message because you are subscribed to the Google Groups "nodejs" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
> To post to this group, send email to nod...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/3A6E789F-9DF7-47AB-9E4B-789209CC32BB%40nbtsc.org.

Aria Stewart

unread,
Sep 30, 2014, 5:27:37 PM9/30/14
to nod...@googlegroups.com

On Sep 30, 2014, at 5:20 PM, Ingwie Phoenix <ingwi...@googlemail.com> wrote:

> I see, thats one solution - and you just reminded me on this hTTP header i keep forgetting too! Thanks for that by the way.
>
> I have to sen dmultiple files and retain some metadata (title, description). Is there some kind of JSON that also lets me work with binary data - in both languages that is?

Not easily: At that point you're into binary serialization formats like bson.

Which really aren't that different from, say, a tar file with metadata in an adjacent file.

Maybe you want attachments as binary, and json that references them:

{
"files": [
{ "name": "foo.jpg", "title": "My File" }
]
}

and then just bundle that all up together.

This is ultimately how npm packages, ruby gems and debian packages work. I think it's the same kind of problem.

Aria
Reply all
Reply to author
Forward
0 new messages