如何从Javascript FileReader base64字符串中删除类型? [英] How to strip type from Javascript FileReader base64 string?

查看:199
本文介绍了如何从Javascript FileReader base64字符串中删除类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Javascript中有以下代码:

I've got the following code in my Javascript:

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

这显示以下数据:

 data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAAAAABX3VL4AAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH3gYSDCUgSze0AAAAAA5JREFUCNdjrGJgYmAAAAJ0AH4SDHVIAAAAAElFTkSuQmCC

在事是我只想要逗号之后的部分。我尝试从 reader.result.value reader.result.valueOf()以及其他一些组合中获取它,但是找不到正确的JUST从逗号后面开始获取base64字符串。所以第二个想法是简单地删除逗号和之前的所有内容,但我不确定如何做到这一点。

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!

推荐答案

以下功能将达到您想要的效果:

The following functions will achieve your desired result:

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

这将字符串拆分为一个字符串数组,第一项(索引0)包含 data:image / png; base64 和包含base64编码数据的第二项(索引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);

请参阅 JSFiddle

这篇关于如何从Javascript FileReader base64字符串中删除类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆