113

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!

1
  • 4
    var base64result = reader.result.split(',')[1]; or var base64result = reader.result.substr(reader.result.indexOf(',') + 1); Commented Jun 18, 2014 at 15:19

5 Answers 5

213

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.

Sign up to request clarification or add additional context in comments.

9 Comments

Do we know for sure, that a comma will never occur in the base64 string it self? If so, very good solutions.
Yes we know for sure. Base64 encoding uses the characters [a-z, A-Z, 0-9, +, /] to encode. = is also used for padding. See Base64.
Excellent. Thank you very much for your explanation.
Thanks ! Before reading this answer I used this: var base64result = reader.result.match(/^data:.+\/(.+);base64,(.*)$/)[2] which is way, way slower. I guess the complexity of RegEx operations are much more comparing to String.split
which one is faster, will split search through the file or quit at the first?
|
28
let reader: FileReader = new FileReader();
 
 reader.onloaded = (e) => {
    let base64String = reader.result.split(',').pop();
 };

or

let base64String = /,(.+)/.exec(reader.result)[1];

1 Comment

I really like the .pop() option vs all other options.
6

You can try splitting your data using ;base64,.

// In here you are getting the data type. Ex - png, jpg, jpeg, etc. You can use this for any further purposes.
var dataType = reader.result.split(';base64,')[0];

// In here you are getting the base64 string and you can use this for your purpose.
var base64result = reader.result.split(';base64,')[1];

Comments

1

You could also do

var base64=reader.result.replace(/^data:image\/\w+;base64,/, "");

1 Comment

this is only for images
0

You can use the following regex to replace any kind of data type:

const base64 = reader.result.toString().replace(/^data:(.*,)?/, "");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.