发送文件
发送一个文件,强制客户端下载。
上下文语境方法
// ServeContent serves content, headers are autoset
// receives three parameters, it's low-level function, instead you can use .ServeFile(string,bool)/SendFile(string,string)
//
// You can define your own "Content-Type" header also, after this function call
// Doesn't implements resuming (by range), use ctx.SendFile instead
ServeContent(content io.ReadSeeker, filename string, modtime time.Time, enableGzip bool) error
// ServeFile serves a view file, to send a file ( zip for example) to the client you should use the SendFile(serverfilename,clientfilename)
// receives two parameters
// filename/path (string)
// gzipCompression (bool)
//
// You can define your own "Content-Type" header also, after this function call
// This function doesn't implement resuming (by range), use ctx.SendFile instead
//
// Use it when you want to serve css/js/... files to the client, for bigger files and 'force-download' use the SendFile
ServeFile(filename string, enableGzip bool) error
// You can define your own "Content-Type" header also, after this function call
// for example: ctx.SetHeader("Content-Type","thecontent/type")
SendFile(filename string, destinationName string)
package main
import "github.com/kataras/iris"
func main() {
iris.Get("/servezip", func(ctx *iris.Context) {
file := "./files/first.zip"
ctx.SendFile(file, "saveAsName.zip")
})
iris.Listen(":8080")
}
您还可以手动发送用户将下载的字节(bytes):
package main
import "github.com/kataras/iris"
func main() {
iris.Get("/servezip", func(ctx *iris.Context) {
// read your file or anything
var binary data[]
ctx.Data(iris.StatusOK, data)
// or ctx.Write(data)
})
iris.Listen(":8080")
}