英文:
Base64 string decode and save as file
问题
这让我很困惑,希望有人能帮助我。如果这是一个愚蠢的问题,请原谅,因为我对Go语言非常陌生。
我有一个包含base64的结构体。结构体如下所示:
type UploadedFile struct {
PartnerId string
FileName string
UploadDateTime string
FileChecksum string
FileBase64 string
}
我想要将base64字符串解码并保存,听起来很简单,但我卡住了。
代码如下:
decoder := json.NewDecoder(r.Body)
uploadedFile := models.UploadedFile{}
err := decoder.Decode(&uploadedFile)
dec, _ := base64.StdEncoding.DecodeString(uploadedFile.FileBase64)
接下来该怎么做呢?我尝试了很多方法,但总是遇到各种错误。
我尝试了适应用于图像的代码,但总是失败,因为文件不是图像,它可以是任何类型的文件。
提前感谢您的帮助。
英文:
This has been doing my head in and I hope someone can help. Please forgive me if it's a stupid question as I am very new to Go.
I have a struct that has base64 in it. the struct looks like this:
type UploadedFile struct {
PartnerId string
FileName string
UploadDateTime string
FileChecksum string
FileBase64 string
}
I want to take that base64 string, decode it and then save it, sounds simple right and it probably is, but I am struck.
The code looks like this:
decoder := json.NewDecoder(r.Body)
uploadedFile := models.UploadedFile{}
err := decoder.Decode(&uploadedFile)
dec, _ := base64.StdEncoding.DecodeString(uploadedFile.FileBase64)
Where do I go from here? I have tried so many things and I just keep getting errors all over the file.
I have tried adapting code that people use for images, but I always crash and burn as the file isn't an image, it could be anything
Thanks in advance.
答案1
得分: 25
更新:我忘了提到,如果你使用f.Write,请确保在写入完成后调用f.Sync,以确保你写入的所有内容都被实际存储。下面是更新后的代码示例。
不确定你的代码示例是否不完整,所以这个答案可能不相关,但是要将解码后的字符串字节保存到文件中,你首先需要打开或创建一个文件,然后将字节写入其中。可以像这样做:
package main
import (
"encoding/base64"
"io"
"os"
)
var b64 = `TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz
c2luZ1BsYXllciBkYXRhLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBw
ZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu
dWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRo
ZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=`
func main() {
dec, err := base64.StdEncoding.DecodeString(b64)
if err != nil {
panic(err)
}
f, err := os.Create("myfilename")
if err != nil {
panic(err)
}
defer f.Close()
if _, err := f.Write(dec); err != nil {
panic(err)
}
if err := f.Sync(); err != nil {
panic(err)
}
}
在这里运行:https://play.golang.org/p/SZVquhZdXC
英文:
Update: I forgot to mention that, if you use f.Write make sure to also call f.Sync after you're done writing to ensure that all the contents you've written are actually stored. The example shows the updated code.
Not sure if your code example is incomplete, so this answer might be irrelevant but to save your decoded string bytes to a file you first need to open or create a file and then write the bytes into it. Something like this:
package main
import (
"encoding/base64"
"io"
"os"
)
var b64 = `TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz
IHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2Yg
dGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu
dWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRo
ZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=`
func main() {
dec, err := base64.StdEncoding.DecodeString(b64)
if err != nil {
panic(err)
}
f, err := os.Create("myfilename")
if err != nil {
panic(err)
}
defer f.Close()
if _, err := f.Write(dec); err != nil {
panic(err)
}
if err := f.Sync(); err != nil {
panic(err)
}
}
Run it here: https://play.golang.org/p/SZVquhZdXC
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论