2015-10-02 03:24:38 +02:00
|
|
|
---
|
|
|
|
title: Streaming File Upload
|
|
|
|
menu:
|
|
|
|
main:
|
|
|
|
parent: recipes
|
|
|
|
---
|
2015-06-30 21:10:35 +02:00
|
|
|
|
|
|
|
- Streaming multipart/form-data file upload
|
|
|
|
- Multiple form fields and files
|
|
|
|
|
2015-07-03 08:21:21 +02:00
|
|
|
## Server
|
|
|
|
|
2015-06-30 21:10:35 +02:00
|
|
|
`server.go`
|
|
|
|
|
|
|
|
```go
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
|
|
|
|
"github.com/labstack/echo"
|
2015-07-02 20:14:09 +02:00
|
|
|
mw "github.com/labstack/echo/middleware"
|
2015-06-30 21:10:35 +02:00
|
|
|
"io"
|
|
|
|
"net/http"
|
2015-07-02 20:14:09 +02:00
|
|
|
"os"
|
2015-06-30 21:10:35 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func upload(c *echo.Context) error {
|
|
|
|
mr, err := c.Request().MultipartReader()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read form field `name`
|
|
|
|
part, err := mr.NextPart()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer part.Close()
|
|
|
|
b, err := ioutil.ReadAll(part)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
name := string(b)
|
|
|
|
|
|
|
|
// Read form field `email`
|
|
|
|
part, err = mr.NextPart()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer part.Close()
|
|
|
|
b, err = ioutil.ReadAll(part)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
email := string(b)
|
|
|
|
|
|
|
|
// Read files
|
|
|
|
i := 0
|
|
|
|
for {
|
|
|
|
part, err := mr.NextPart()
|
|
|
|
if err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer part.Close()
|
|
|
|
|
|
|
|
file, err := os.Create(part.FileName())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
if _, err := io.Copy(file, part); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
return c.String(http.StatusOK, "Thank You! %s <%s>, %d files uploaded successfully.",
|
|
|
|
name, email, i)
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
e := echo.New()
|
2015-07-02 20:14:09 +02:00
|
|
|
e.Use(mw.Logger())
|
|
|
|
e.Use(mw.Recover())
|
|
|
|
|
|
|
|
e.Static("/", "public")
|
2015-06-30 21:10:35 +02:00
|
|
|
e.Post("/upload", upload)
|
2015-07-02 20:14:09 +02:00
|
|
|
|
2015-06-30 21:10:35 +02:00
|
|
|
e.Run(":1323")
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2015-07-03 08:21:21 +02:00
|
|
|
## Client
|
|
|
|
|
2015-06-30 21:10:35 +02:00
|
|
|
`index.html`
|
|
|
|
|
|
|
|
```html
|
|
|
|
<!doctype html>
|
|
|
|
<html lang="en">
|
|
|
|
<head>
|
|
|
|
<meta charset="utf-8">
|
|
|
|
<title>File Upload</title>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<h1>Upload Files</h1>
|
|
|
|
<form action="/upload" method="post" enctype="multipart/form-data">
|
|
|
|
Name: <input type="text" name="name"><br>
|
|
|
|
Email: <input type="email" name="email"><br>
|
|
|
|
Files: <input type="file" name="files" multiple><br><br>
|
|
|
|
<input type="submit" value="Submit">
|
|
|
|
</form>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
## [Source Code](https://github.com/labstack/echo/blob/master/recipes/streaming-file-upload)
|