2015-10-01 18:24:38 -07:00
|
|
|
---
|
|
|
|
title: File Upload
|
|
|
|
menu:
|
2015-10-08 13:54:31 -07:00
|
|
|
side:
|
2015-10-07 15:41:15 -07:00
|
|
|
parent: recipes
|
2015-10-08 13:54:31 -07:00
|
|
|
weight: 7
|
2015-10-01 18:24:38 -07:00
|
|
|
---
|
2015-06-30 12:10:35 -07: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-01 18:24:38 -07:00
|
|
|
us an option to specify the maximum memory used while parsing the request body.
|
2015-06-30 11:51:08 -07:00
|
|
|
|
2015-11-16 07:57:40 -08: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-07 17:04:51 -07:00
|
|
|
### Server
|
2015-07-02 23:21:21 -07:00
|
|
|
|
2015-06-30 11:51:08 -07:00
|
|
|
`server.go`
|
|
|
|
|
2015-10-08 13:54:31 -07:00
|
|
|
{{< embed "file-upload/server.go" >}}
|
2015-06-30 11:51:08 -07:00
|
|
|
|
2015-10-07 17:04:51 -07:00
|
|
|
### Client
|
2015-07-02 23:21:21 -07:00
|
|
|
|
2015-06-30 11:51:08 -07:00
|
|
|
`index.html`
|
|
|
|
|
2015-10-08 13:54:31 -07:00
|
|
|
{{< embed "file-upload/public/index.html" >}}
|
2015-06-30 11:51:08 -07:00
|
|
|
|
2015-10-07 17:04:51 -07:00
|
|
|
### Maintainers
|
2015-10-07 15:41:15 -07:00
|
|
|
|
2015-10-08 13:54:31 -07:00
|
|
|
- [vishr](https://github.com/vishr)
|
2015-10-07 15:41:15 -07:00
|
|
|
|
2016-03-05 14:34:34 -08:00
|
|
|
### [Source Code](https://github.com/vishr/echo-recipes/blob/master/v1/file-upload)
|