update the code style

This commit is contained in:
astaxie
2015-04-02 13:22:13 +08:00
committed by James Miranda
parent 7fda55b543
commit efb458cd1b

View File

@@ -4,31 +4,38 @@ Suppose you have a website like Instagram and you want users to upload their bea
You have to add property `enctype` to the form that you want to use for uploading photos. There are three possible values for this property: You have to add property `enctype` to the form that you want to use for uploading photos. There are three possible values for this property:
application/x-www-form-urlencoded Transcode all characters before uploading (default). ```
multipart/form-data No transcoding. You must use this value when your form has file upload controls. application/x-www-form-urlencoded Transcode all characters before uploading (default).
text/plain Convert spaces to "+", but no transcoding for special characters. multipart/form-data No transcoding. You must use this value when your form has file upload controls.
text/plain Convert spaces to "+", but no transcoding for special characters.
```
Therefore, the HTML content of a file upload form should look like this: Therefore, the HTML content of a file upload form should look like this:
<html> ```
<head> <html>
<head>
<title>Upload file</title> <title>Upload file</title>
</head> </head>
<body> <body>
<form enctype="multipart/form-data" action="http://127.0.0.1:9090/upload" method="post"> <form enctype="multipart/form-data" action="http://127.0.0.1:9090/upload" method="post">
<input type="file" name="uploadfile" /> <input type="file" name="uploadfile" />
<input type="hidden" name="token" value="{{.}}"/> <input type="hidden" name="token" value="{{.}}"/>
<input type="submit" value="upload" /> <input type="submit" value="upload" />
</form> </form>
</body> </body>
</html> </html>
```
We need to add a function on the server side to handle this form. We need to add a function on the server side to handle this form.
http.HandleFunc("/upload", upload) ```
http.HandleFunc("/upload", upload)
// upload logic // upload logic
func upload(w http.ResponseWriter, r *http.Request) { func upload(w http.ResponseWriter, r *http.Request) {
fmt.Println("method:", r.Method) fmt.Println("method:", r.Method)
if r.Method == "GET" { if r.Method == "GET" {
crutime := time.Now().Unix() crutime := time.Now().Unix()
@@ -55,7 +62,9 @@ We need to add a function on the server side to handle this form.
defer f.Close() defer f.Close()
io.Copy(f, file) io.Copy(f, file)
} }
} }
```
As you can see, we need to call `r.ParseMultipartForm` for uploading files. The function `maxMemory` argument. After you call `ParseMultipartForm`, the file will be saved in the server memory with `maxMemory` size. If the file size is larger than `maxMemory`, the rest of the data will be saved in a system temporary file. You can use `r.FormFile` to get the file handle and use `io.Copy` to save to your file system. As you can see, we need to call `r.ParseMultipartForm` for uploading files. The function `maxMemory` argument. After you call `ParseMultipartForm`, the file will be saved in the server memory with `maxMemory` size. If the file size is larger than `maxMemory`, the rest of the data will be saved in a system temporary file. You can use `r.FormFile` to get the file handle and use `io.Copy` to save to your file system.
@@ -69,11 +78,13 @@ We use three steps for uploading files as follows:
The file handler is the `multipart.FileHeader`. It uses the following struct: The file handler is the `multipart.FileHeader`. It uses the following struct:
type FileHeader struct { ```
type FileHeader struct {
Filename string Filename string
Header textproto.MIMEHeader Header textproto.MIMEHeader
// contains filtered or unexported fields // contains filtered or unexported fields
} }
```
![](images/4.5.upload2.png?raw=true) ![](images/4.5.upload2.png?raw=true)
@@ -83,9 +94,10 @@ Figure 4.5 Print information on server after receiving file.
I showed an example of using a form to a upload a file. We can impersonate a client form to upload files in Go as well. I showed an example of using a form to a upload a file. We can impersonate a client form to upload files in Go as well.
package main ```
package main
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"io" "io"
@@ -93,9 +105,9 @@ I showed an example of using a form to a upload a file. We can impersonate a cli
"mime/multipart" "mime/multipart"
"net/http" "net/http"
"os" "os"
) )
func postFile(filename string, targetUrl string) error { func postFile(filename string, targetUrl string) error {
bodyBuf := &bytes.Buffer{} bodyBuf := &bytes.Buffer{}
bodyWriter := multipart.NewWriter(bodyBuf) bodyWriter := multipart.NewWriter(bodyBuf)
@@ -134,14 +146,16 @@ I showed an example of using a form to a upload a file. We can impersonate a cli
fmt.Println(resp.Status) fmt.Println(resp.Status)
fmt.Println(string(resp_body)) fmt.Println(string(resp_body))
return nil return nil
} }
// sample usage // sample usage
func main() { func main() {
target_url := "http://localhost:9090/upload" target_url := "http://localhost:9090/upload"
filename := "./astaxie.pdf" filename := "./astaxie.pdf"
postFile(filename, target_url) postFile(filename, target_url)
} }
```
The above example shows you how to use a client to upload files. It uses `multipart.Write` to write files into cache and sends them to the server through the POST method. The above example shows you how to use a client to upload files. It uses `multipart.Write` to write files into cache and sends them to the server through the POST method.