Base64字符串解码并保存为文件

huangapple go评论95阅读模式
英文:

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

huangapple
  • 本文由 发表于 2017年4月4日 23:59:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/43212213.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定