2016-12-15 7 views
1

キャンバスイメージをpng形式で保存して、それを自分のサーバーに送信します。私が送っている文字列はこの形式です...move_uploaded _fileに相当する

data:image/png;base64,iVBORw0K... 

このイメージを自分のサーバー上のimagesという名前のフォルダに保存します。しかし、私が例を探すとき、私は何も見つけることができません。私は基本的にmove_uploaded_fileが行うことをPHPでやりたいと思っています。今、私は今、私は、そのフォルダを開いたときに、私は、画像を見ることができるように私は私のサーバー内のフォルダに、上記の文字列の形式である、img.Testを救うだろうか...

type test_struct struct { 
    Test string `json:"image"` 
} 

func GetImage(rw http.ResponseWriter, req *http.Request, _ httprouter.Params) { 

    var img test_struct 
    json.NewDecoder(req.Body).Decode(&img) 

これを持っていますか?

答えて

1

ファイルがbase64でエンコードされている文字列がすでにある場合は、デコードして[]byteデータを取得し、最後にファイルを書き込む必要があります。

position := strings.Index(img.Test, ",") 
if position == -1 { 
    // image format doesn't match with 'data:image/png;base64,iVBO...' 
} 
// decode the base64 string, removing 'data:image/png;base64,' from it 
reader := base64.NewDecoder(base64.StdEncoding, bytes.NewBufferString(img.Image[position+1:])) 

data, err := ioutil.ReadAll(reader) 
if err != nil { 
    // error handler 
} 
// you write the file in wherever you want 
ioutil.WriteFile("./image.png", data, 0644)