Base64 string decode and save as file

15,284

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

Share:
15,284
Paul A.T. Wilson
Author by

Paul A.T. Wilson

Updated on July 28, 2022

Comments

  • Paul A.T. Wilson
    Paul A.T. Wilson almost 2 years

    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.

  • Paul A.T. Wilson
    Paul A.T. Wilson about 7 years
    What if the file that is encoded is a PDF or a .tar.gz can I sill use WriteString? I might have completely missed the point but I thought WriteString was just for writing a text file?
  • mkopriva
    mkopriva about 7 years
    Are you looking for how to write bytes into a file? Something like this? play.golang.org/p/QfY5leW4O9 I'll update my answer if that's the case.
  • Paul A.T. Wilson
    Paul A.T. Wilson about 7 years
    I actually used your example and it worked for a .gz file, a pdf, an image and a HUGE binary file. So it appears I was just missing the obvious on this one.
  • mkopriva
    mkopriva about 7 years
    Ok, glad you got it working, I've updated the answer anyway to write bytes instead of a string which is the way to go, I think. I don't know why I used io.WriteString first :F