【问题标题】:Saving Images in Golang from Base64 String从 Base64 字符串保存 Golang 中的图像
【发布时间】:2020-09-17 01:08:40
【问题描述】:

我正在使用 Wacom 签名集将 JS 中的签名捕获为 Base64 字符串。然后我将此 Base64 字符串发送到我的 Golang 服务器并将其保存到数据库中。 然后,当我想将此 Base64 字符串另存为图像时,我总是收到错误“Bad png”。 所有转换器都可以毫无问题地打开 Base64 字符串。 这里以我的函数和 Base64 字符串为例。

func saveImageTest() 
{
im1 := "" //Link to the string is below
idx := strings.Index(im1, ";base64,")
if idx < 0 {
    fmt.Println("InvalidImage")
}
ImageType := im1[11:idx]
fmt.Println(ImageType)
unbased, err := base64.StdEncoding.DecodeString(im1)
if err != nil {
    log.Println("Cannot decode b64")
}
    r := bytes.NewReader(unbased)

    im, err := png.Decode(r)
    if err != nil {
        log.Println("Bad png")
    }

    f, err := os.OpenFile("example.png", os.O_WRONLY|os.O_CREATE, 0777)
    if err != nil {
        log.Println("Cannot open file")
    }
    png.Encode(f, im)

}

Link to the Base64 String Test signature

【问题讨论】:

    标签: base64 png


    【解决方案1】:

    您必须从 idx + 8 开始 DecodeString(在“;base64”之后)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-11
      • 2015-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-28
      • 1970-01-01
      相关资源
      最近更新 更多