I am getting: error: illegal base64 data at input byte 4
When passing in Base64Image into base64.StdEncoding.DecodeString(str)
:
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAYA...
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?
Not all of your input string you try to decode is Base64 encoded form.
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.
It has a format of:
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.To acquire the data (that is the Base64 encoded form), cut off the prefix up to the comma (comma included):
Output:
Of which you can now decode:
Output:
Try it on the Go Playground.
It's because your string isn't in base64 until after the comma data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAYA...
import "strings" and use split to get the half after the comma and then call decodestring with that.
EDIT: made the split token
base64,
because it's more specific to your input.Sometimes this happens if your base64 string is not properly padded with == at the end.