how to convert image to base64 js

515 views
Skip to first unread message

victor.t...@gmail.com

unread,
Apr 25, 2015, 12:06:31 PM4/25/15
to ve...@googlegroups.com
hello try to convert image to base64 and send to twitter 

 vertx.fileSystem.readFile('media/test5.jpg', function(err, res) {
                if (!err) {   
               //res is a buffer   .toString('utf-16'); in nodejs have 'base64' option    
              //vertx has a similar option ? only worked utf8 and utf16
                 var resString=res.toString();  //    .toString() return ï¿½ï¿½ï¿½ï¿½\u0000\u0010JFIF.............          
                resString = rstr2b64(resString);//my function encode_base64() work fine but dont return correct string ÿØÿà JFIF.........
                     replier(resString);
                }
                if (err){
                    console.log("ERROR"+err.toString());
                }
            });

Alexander Lehmann

unread,
Apr 25, 2015, 5:44:42 PM4/25/15
to ve...@googlegroups.com
You can use the base64 class from jdk if you are using java8, something like:

Base64.getEncoder().encodeToString(res.getBytes());

Alexander Lehmann

unread,
Apr 25, 2015, 5:54:17 PM4/25/15
to ve...@googlegroups.com
sorry, that only works in Java

victor.t...@gmail.com

unread,
Apr 25, 2015, 5:59:01 PM4/25/15
to ve...@googlegroups.com
im using js but is possible add a class from java8 in js ?

Tim Fox

unread,
Apr 26, 2015, 4:36:34 AM4/26/15
to ve...@googlegroups.com
There's more information on this in the Nashorn docs, but you should be able to do something like:

java.util.Base64.getEncoder()

to get an encoder

https://docs.oracle.com/javase/8/docs/technotes/guides/scripting/nashorn/api.html
--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Julien Viet

unread,
Apr 26, 2015, 4:42:14 AM4/26/15
to ve...@googlegroups.com, Tim Fox
I think one issue will be to extract the byte[] from the wrapped JS Buffer, unless the delegate is used.
-- 
Julien Viet
www.julienviet.com

Tim Fox

unread,
Apr 26, 2015, 4:51:22 AM4/26/15
to Julien Viet, ve...@googlegroups.com
Surely there's a JS Base64 library somewhere?

Clement Escoffier

unread,
Apr 26, 2015, 4:55:18 AM4/26/15
to ve...@googlegroups.com
Hi,

Yes, there are methods implemented natively by “recent” browsers:

Seems to be largely supported:

Cheers,

Clement

victor.t...@gmail.com

unread,
Apr 29, 2015, 7:29:23 PM4/29/15
to ve...@googlegroups.com, timv...@gmail.com

I think one issue will be to extract the byte[] from the wrapped JS Buffer, unless the delegate is used.
-- 
Julien Viet
www.julienviet.com

tnks  i get byte array from buffer and encode b64  my mod for twitter is completed =P
vertx.fileSystem.readFile(img.path, function(err, res) {
            if (!err) {              
                var resByte = [];
                for (var i = 0; i < res.length(); i++) {
                    resByte[i] = res.getByte(i);
                }
                var imgdata = "--" + boundary + "\r\n" +
                    'Content-Disposition: form-data; name="media_data"\r\n\r\n' +
                    base64ArrayBuffer(resByte) + "\r\n" +
                    '--' + boundary + '--';             
                doRequest(method, path, oAuth, imgdata, replier);
            } else {
                replier("ERROR: " + err.toString());
            }
        });
Reply all
Reply to author
Forward
0 new messages