2015-10-02 03:24:38 +02:00
|
|
|
---
|
|
|
|
title: File Upload
|
|
|
|
menu:
|
2015-10-08 22:54:31 +02:00
|
|
|
side:
|
2015-10-08 00:41:15 +02:00
|
|
|
parent: recipes
|
2015-10-08 22:54:31 +02:00
|
|
|
weight: 7
|
2015-10-02 03:24:38 +02:00
|
|
|
---
|
2015-06-30 21:10:35 +02:00
|
|
|
|
|
|
|
- Multipart/form-data file upload
|
|
|
|
- Multiple form fields and files
|
|
|
|
|
|
|
|
Use `req.ParseMultipartForm(16 << 20)` for manually parsing multipart form. It gives
|
2015-10-02 03:24:38 +02:00
|
|
|
us an option to specify the maximum memory used while parsing the request body.
|
2015-06-30 20:51:08 +02:00
|
|
|
|
2015-11-16 17:57:40 +02:00
|
|
|
If you just want to upload a single file:
|
|
|
|
|
|
|
|
```go
|
|
|
|
file, fh, err := req.FormFile("file")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
// Destination
|
|
|
|
dst, err := os.Create(fh.Filename)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer dst.Close()
|
|
|
|
|
|
|
|
// Copy
|
|
|
|
if _, err = io.Copy(dst, file); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2015-10-08 02:04:51 +02:00
|
|
|
### Server
|
2015-07-03 08:21:21 +02:00
|
|
|
|
2015-06-30 20:51:08 +02:00
|
|
|
`server.go`
|
|
|
|
|
2015-10-08 22:54:31 +02:00
|
|
|
{{< embed "file-upload/server.go" >}}
|
2015-06-30 20:51:08 +02:00
|
|
|
|
2015-10-08 02:04:51 +02:00
|
|
|
### Client
|
2015-07-03 08:21:21 +02:00
|
|
|
|
2015-06-30 20:51:08 +02:00
|
|
|
`index.html`
|
|
|
|
|
2015-10-08 22:54:31 +02:00
|
|
|
{{< embed "file-upload/public/index.html" >}}
|
2015-06-30 20:51:08 +02:00
|
|
|
|
2015-10-08 02:04:51 +02:00
|
|
|
### Maintainers
|
2015-10-08 00:41:15 +02:00
|
|
|
|
2015-10-08 22:54:31 +02:00
|
|
|
- [vishr](https://github.com/vishr)
|
2015-10-08 00:41:15 +02:00
|
|
|
|
2016-03-06 19:52:32 +02:00
|
|
|
### [Source Code](https://github.com/vishr/echo-recipes/blob/master/v2/file-upload)
|