Serve files from Context
1
// SendFile sends a file as an attachment, that is downloaded and
2
// saved locally from client.
3
// Note that compression can be registered through `ctx.CompressWriter(true)`
4
// or `app.Use(iris.Compression)`.
5
// Use `ServeFile` if a file should be served as a page asset instead.
6
SendFile(filename string, destinationName string) error
7
// SendFileWithRate same as `SendFile` but it can throttle the speed of reading
8
// and though writing the file to the client.
9
SendFileWithRate(src, destName string, limit float64, burst int) error
Copied!
Usage
Force-Send a file to the client:
1
func handler(ctx iris.Context) {
2
src := "./files/first.zip"
3
ctx.SendFile(src, "client.zip")
4
}
Copied!
Limit download speed to ~50Kb/s with a burst of 100KB:
1
func handler(ctx iris.Context) {
2
src := "./files/big.zip"
3
// optionally, keep it empty to resolve the filename based on the "src".
4
dest := ""
5
6
limit := 50.0 * iris.KB
7
burst := 100 * iris.KB
8
ctx.SendFileWithRate(src, dest, limit, burst)
9
}
Copied!
1
// ServeContent replies to the request using the content in the
2
// provided ReadSeeker. The main benefit of ServeContent over io.Copy
3
// is that it handles Range requests properly, sets the MIME type, and
4
// handles If-Match, If-Unmodified-Since, If-None-Match, If-Modified-Since,
5
// and If-Range requests.
6
//
7
// If the response's Content-Type header is not set, ServeContent
8
// first tries to deduce the type from name's file extension.
9
//
10
// The name is otherwise unused; in particular it can be empty and is
11
// never sent in the response.
12
//
13
// If modtime is not the zero time or Unix epoch, ServeContent
14
// includes it in a Last-Modified header in the response. If the
15
// request includes an If-Modified-Since header, ServeContent uses
16
// modtime to decide whether the content needs to be sent at all.
17
//
18
// The content's Seek method must work: ServeContent uses
19
// a seek to the end of the content to determine its size.
20
//
21
// If the caller has set w's ETag header formatted per RFC 7232, section 2.3,
22
// ServeContent uses it to handle requests using If-Match, If-None-Match, or If-Range.
23
//
24
// Note that *os.File implements the io.ReadSeeker interface.
25
// Note that compression can be registered through
26
// `ctx.CompressWriter(true)` or `app.Use(iris.Compression)`.
27
ServeContent(content io.ReadSeeker, filename string, modtime time.Time)
28
// ServeContentWithRate same as `ServeContent` but it can throttle the speed of reading
29
// and though writing the "content" to the client.
30
ServeContentWithRate(content io.ReadSeeker, filename string, modtime time.Time, limit float64, burst int)
31
// ServeFile replies to the request with the contents of the named
32
// file or directory.
33
//
34
// If the provided file or directory name is a relative path, it is
35
// interpreted relative to the current directory and may ascend to
36
// parent directories. If the provided name is constructed from user
37
// input, it should be sanitized before calling `ServeFile`.
38
//
39
// Use it when you want to serve assets like css and javascript files.
40
// If client should confirm and save the file use the `SendFile` instead.
41
// Note that compression can be registered through
42
// `ctx.CompressWriter(true)`
43
// or `app.Use(iris.Compression)`.
44
ServeFile(filename string) error
45
// ServeFileWithRate same as `ServeFile` but it can throttle the speed of reading
46
// and though writing the file to the client.
47
ServeFileWithRate(filename string, limit float64, burst int) error
Copied!
Usage
1
func handler(ctx iris.Context) {
2
ctx.ServeFile("./public/main.js")
3
}
Copied!
Last modified 1yr ago
Export as PDF
Copy link