在输入字节4非法的base64数据使用base64.StdEncoding.De codeString的时候(STR) [英] Illegal base64 data at input byte 4 when using base64.StdEncoding.DecodeString(str)
问题描述
我收到:错误:在输入字节的base64非法数据4
当传递到Base64Image base64.StdEncoding.De codeString的(STR)
:
When passing in Base64Image into base64.StdEncoding.DecodeString(str)
:
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAYA...
让我知道如果你需要完整的base64,我刚才粘贴在第一部分,因为它看起来像这个问题是4字节中?
Let me know if you need the full base64, I have just pasted in the first part as it looks like the problem is within 4 byte?
data, errBase := base64.StdEncoding.DecodeString(Base64Image)
if errBase != nil {
fmt.Println("error:", errBase)
return false
}
你知道为什么吗?
Do you know why?
在此先感谢
推荐答案
不是所有的输入字符串尝试去code为Base64编码的连接codeD的形式。
Not all of your input string you try to decode is Base64 encoded form.
你有什么是数据URI方案,提供了一个办法,包括数据IN-线在网页中,好像他们是外部资源。
What you have is a Data URI scheme, that provides a way to include data in-line in web pages as if they were external resources.
它的格式为:
data:[<MIME-type>][;charset=<encoding>][;base64],<data>
如果你的情况图像/ PNG
是MIME类型,可选字符集失踪,的base64
是指示常量字符串&lt;数据方式&gt;
是连接使用Base64编码codeD
Where in your case image/png
is the MIME-type, the optional charset is missing, and ";base64"
is a constant string indicating that <data>
is encoded using Base64 encoding.
要获得的数据(也就是为Base64 EN $ C $光盘形式),切断preFIX到逗号(包括逗号):
To acquire the data (that is the Base64 encoded form), cut off the prefix up to the comma (comma included):
input := "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAYA"
b64data := input[strings.IndexByte(input, ',')+1:]
fmt.Println(b64data)
输出:
iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAYA
其中,你现在可以去code:
Of which you can now decode:
data, err := base64.StdEncoding.DecodeString(b64data)
if err != nil {
fmt.Println("error:", err)
}
fmt.Println(data)
输出:
[137 80 78 71 13 10 26 10 0 0 0 13 73 72 68 82 0 0 0 100 0 0 0 100 8 6 0]
试试看的<大骨节病> 去游乐场 骨节病>。
这篇关于在输入字节4非法的base64数据使用base64.StdEncoding.De codeString的时候(STR)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!