将 base64 转换为图像并保存在本地的最佳方法是什么?
what is the best way to convert base64 to image and save it locally?
我从客户端收到的照片是base64转换的,现在我要将base64解码成图片并保存在本地文件夹中,我该怎么办?
this code doesn't work.
您的代码无法编译; base64.NewDecoder
returns an io.Reader
; you cannot use []byte()
to convert that into a byte slice (ioutil.ReadAll
可以为您做到这一点)。但是没有必要这样做;您可以将 Reader
复制到文件:
dec := base64.NewDecoder(base64.StdEncoding, strings.NewReader(photo[i+1:]))
f, err := os.Create("/var/www/upload/" + req.Title + ".png")
if err != nil {
panic(err)
}
defer f.Close()
_, err = io.Copy(f, dec)
if err != nil {
panic(err)
}
我从客户端收到的照片是base64转换的,现在我要将base64解码成图片并保存在本地文件夹中,我该怎么办?
this code doesn't work.
您的代码无法编译; base64.NewDecoder
returns an io.Reader
; you cannot use []byte()
to convert that into a byte slice (ioutil.ReadAll
可以为您做到这一点)。但是没有必要这样做;您可以将 Reader
复制到文件:
dec := base64.NewDecoder(base64.StdEncoding, strings.NewReader(photo[i+1:]))
f, err := os.Create("/var/www/upload/" + req.Title + ".png")
if err != nil {
panic(err)
}
defer f.Close()
_, err = io.Copy(f, dec)
if err != nil {
panic(err)
}