Merge commit '01ace5c50ddaad75486ebea75c2aa4a23c021b6b' into ja
This commit is contained in:
@@ -1,14 +1,14 @@
|
|||||||
# 4.5 File upload
|
# 4.5 File upload
|
||||||
|
|
||||||
Suppose you have a website like Instagram, and you want users to upload their beautiful photos, how you are going to do?
|
Suppose you have a website like Instagram and you want users to upload their beautiful photos. How would you implement that functionality?
|
||||||
|
|
||||||
You have to add property `enctype` to the form that you want to use for uploading photos, and there are three possibilities for its value:
|
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 Trans-coding all characters before upload(default).
|
application/x-www-form-urlencoded Transcode all characters before uploading (default).
|
||||||
multipart/form-data No trans-coding, you have to use this value when your form have file upload controls.
|
multipart/form-data No transcoding. You must use this value when your form has file upload controls.
|
||||||
text/plain Convert spaces to "+", but no trans-coding for special characters.
|
text/plain Convert spaces to "+", but no transcoding for special characters.
|
||||||
|
|
||||||
Therefore, 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>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
@@ -23,7 +23,7 @@ Therefore, HTML content of a file upload form should look like this:
|
|||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
||||||
We need to add a function in server side to handle this affair.
|
We need to add a function on the server side to handle this form.
|
||||||
|
|
||||||
http.HandleFunc("/upload", upload)
|
http.HandleFunc("/upload", upload)
|
||||||
|
|
||||||
@@ -57,17 +57,17 @@ We need to add a function in server side to handle this affair.
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
As you can see, we need to call `r.ParseMultipartForm` for uploading files, the argument means the `maxMemory`. After you called `ParseMultipartForm`, file will be saved in your server memory with `maxMemory` size, if the file size is larger than `maxMemory`, rest of data will be saved in system temporary file. You can use `r.FormFile` to get 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.
|
||||||
|
|
||||||
You don't need to call `r.ParseForm` when you access other non-file fields in the form because Go will call it when it's necessary. Also call `ParseMultipartForm` once is enough, and no differences for multiple calls.
|
You don't need to call `r.ParseForm` when you access other non-file fields in the form because Go will call it when it's necessary. Also, calling `ParseMultipartForm` once is enough -multiple calls make no difference.
|
||||||
|
|
||||||
We use three steps for uploading files as follows:
|
We use three steps for uploading files as follows:
|
||||||
|
|
||||||
1. Add `enctype="multipart/form-data"` to your form.
|
1. Add `enctype="multipart/form-data"` to your form.
|
||||||
2. Call `r.ParseMultipartForm` in server side to save file in memory or temporary file.
|
2. Call `r.ParseMultipartForm` on the server side to save the file either to memory or to a temporary file.
|
||||||
3. Call `r.FormFile` to get file handle and save to file system.
|
3. Call `r.FormFile` to get the file handle and save to the file system.
|
||||||
|
|
||||||
The file handler is the `multipart.FileHeader`, it uses following struct:
|
The file handler is the `multipart.FileHeader`. It uses the following struct:
|
||||||
|
|
||||||
type FileHeader struct {
|
type FileHeader struct {
|
||||||
Filename string
|
Filename string
|
||||||
@@ -77,11 +77,11 @@ The file handler is the `multipart.FileHeader`, it uses following struct:
|
|||||||
|
|
||||||

|

|
||||||
|
|
||||||
Figure 4.5 Print information in server after received file.
|
Figure 4.5 Print information on server after receiving file.
|
||||||
|
|
||||||
## Clients upload files
|
## Clients upload files
|
||||||
|
|
||||||
I showed the example of using form to upload file, and we can impersonate a client form to upload file 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
|
||||||
|
|
||||||
@@ -143,12 +143,12 @@ I showed the example of using form to upload file, and we can impersonate a clie
|
|||||||
postFile(filename, target_url)
|
postFile(filename, target_url)
|
||||||
}
|
}
|
||||||
|
|
||||||
The above example shows you how to use client to upload file, it uses `multipart.Write` to write file in cache and sends to server through 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.
|
||||||
|
|
||||||
If you have other field need to write into data like user name, call `multipart.WriteField` as needed.
|
If you have other fields that need to write into data, like username, call `multipart.WriteField` as needed.
|
||||||
|
|
||||||
## Links
|
## Links
|
||||||
|
|
||||||
- [Directory](preface.md)
|
- [Directory](preface.md)
|
||||||
- Previous section: [Duplicate submissions](04.4.md)
|
- Previous section: [Duplicate submissions](04.4.md)
|
||||||
- Next section: [Summary](04.6.md)
|
- Next section: [Summary](04.6.md)
|
||||||
|
|||||||
Reference in New Issue
Block a user