Nodejs equivalent of Web Javascript URL.createObjectURL(new Blob(["some string code"]))

2,216 views
Skip to first unread message

DrLightman

unread,
Nov 14, 2017, 12:58:39 PM11/14/17
to nodejs
Hello,

I need to convert for my nodejs project what I found in a javascript environment for web code:

var foo = window.URL.createObjectURL(new Blob([" ..."]) );

Searched but with no luck at all.

Any idea?

Thank you.

kai zhu

unread,
Nov 14, 2017, 10:53:59 PM11/14/17
to nod...@googlegroups.com
here's a quick-and-dirty shell-script which calls nodejs to convert an
image into a datauri (see screenshot for example usage). you can
probably edit it to suit your needs.

(original source-code from
https://github.com/kaizhu256/node-utility2/blob/2017.10.17/lib.utility2.sh#L2052)

-kai

```shell
shImageToDataUri() {(set -e
# this function will convert the image $FILE to a data-uri string
# example usage:
# $ curl -O https://cdn0.iconfinder.com/data/icons/iconico-3/1024/63.png
# $ shImageToDataUri 63.png
case "$1" in
http://*)
FILE=/tmp/shImageToDataUri.png
curl -#Lf "$1" > "$FILE"
;;
https://*)
FILE=/tmp/shImageToDataUri.png
curl -#Lf "$1" > "$FILE"
;;
*)
FILE="$1"
;;
esac
node -e "
// <script>
/*jslint
bitwise: true,
browser: true,
maxerr: 8,
maxlen: 100,
node: true,
nomen: true,
regexp: true,
stupid: true
*/
'use strict';
console.log('data:image/' +
require('path').extname('$FILE').slice(1) +
';base64,' +
require('fs').readFileSync('$FILE').toString('base64'));
// </script>
"
)}
```
> --
> 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/00968901-92ed-4607-98cf-39cf64f3e340%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>
Screen Shot 2017-11-15 at 4.35.30 AM.png
Reply all
Reply to author
Forward
0 new messages