Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
0
rated 0 times [  4] [ 4]  / answers: 1 / hits: 90010  / 10 Years ago, wed, june 18, 2014, 12:00:00

I've got the following code in my Javascript:



var reader = new FileReader();
reader.onloadend = function () {
alert(reader.result);
};


This shows me the following data:



 data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAAAAABX3VL4AAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH3gYSDCUgSze0AAAAAA5JREFUCNdjrGJgYmAAAAJ0AH4SDHVIAAAAAElFTkSuQmCC


The thing is that I only want the part after the comma. I tried getting it from reader.result.value, reader.result.valueOf() and some other combinations, but can't find the correct one to JUST get the base64 string starting from after the comma. So a second idea is to simply strip off the comma and all that is before that, but I'm kind of unsure how to do that.



Would anybody have any idea how to get this done? All tips are welcome!


More From » base64

 Answers
19

The following functions will achieve your desired result:



var base64result = reader.result.split(',')[1];


This splits the string into an array of strings with the first item (index 0) containing data:image/png;base64 and the second item (index 1) containing the base64 encoded data.



Another solution is to find the index of the comma and then simply cut off everything before and including the comma:



var base64result = reader.result.substr(reader.result.indexOf(',') + 1);


See JSFiddle.


[#70521] Monday, June 16, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jennie

Total Points: 593
Total Questions: 102
Total Answers: 106

Location: Federated States of Micronesia
Member since Fri, Sep 16, 2022
2 Years ago
jennie questions
;