Finished translation of Chapter 1 in pt-br
This commit is contained in:
46
en/code/src/apps/ch.4.5/client_upload/main.go
Normal file
46
en/code/src/apps/ch.4.5/client_upload/main.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
func checkError(err error) {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
func postFile(filename string, targetUrl string) {
|
||||
bodyBuf := &bytes.Buffer{}
|
||||
bodyWriter := multipart.NewWriter(bodyBuf)
|
||||
fileWriter, err := bodyWriter.CreateFormFile("uploadfile", filename)
|
||||
checkError(err)
|
||||
|
||||
fh, err := os.Open(filename)
|
||||
checkError(err)
|
||||
|
||||
_, err = io.Copy(fileWriter, fh)
|
||||
checkError(err)
|
||||
|
||||
contentType := bodyWriter.FormDataContentType()
|
||||
bodyWriter.Close()
|
||||
resp, err := http.Post(targetUrl, contentType, bodyBuf)
|
||||
checkError(err)
|
||||
|
||||
defer resp.Body.Close()
|
||||
resp_body, err := ioutil.ReadAll(resp.Body)
|
||||
checkError(err)
|
||||
|
||||
fmt.Println(resp.Status)
|
||||
fmt.Println(string(resp_body))
|
||||
}
|
||||
func main() {
|
||||
target_url := "http://localhost:9090/upload"
|
||||
filename := "../file.txt"
|
||||
postFile(filename, target_url)
|
||||
}
|
||||
Reference in New Issue
Block a user