File Download
How to download a file?
Server
server.go
package main
import (
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
)
func main() {
e := echo.New()
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.GET("/", func(c echo.Context) error {
return c.File("index.html")
})
e.GET("/file", func(c echo.Context) error {
return c.File("echo.svg")
})
e.Logger.Fatal(e.Start(":1323"))
}
Client
index.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>File download</title>
</head>
<body>
<p>
<a href="/file">File download</a>
</p>
</body>
</html>
How to download a file as inline, opening it in the browser?
Server
server.go
package main
import (
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
)
func main() {
e := echo.New()
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.GET("/", func(c echo.Context) error {
return c.File("index.html")
})
e.GET("/inline", func(c echo.Context) error {
return c.Inline("inline.txt", "inline.txt")
})
e.Logger.Fatal(e.Start(":1323"))
}
Client
index.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>File download</title>
</head>
<body>
<p>
<a href="/inline">Inline file download</a>
</p>
</body>
</html>
How to download a file as attachment, prompting client to save the file?
Server
server.go
package main
import (
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
)
func main() {
e := echo.New()
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.GET("/", func(c echo.Context) error {
return c.File("index.html")
})
e.GET("/attachment", func(c echo.Context) error {
return c.Attachment("attachment.txt", "attachment.txt")
})
e.Logger.Fatal(e.Start(":1323"))
}
Client
index.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>File download</title>
</head>
<body>
<p>
<a href="/attachment">Attachment file download</a>
</p>
</body>
</html>