mirror of
https://gitee.com/goploy/goploy.git
synced 2024-12-02 20:20:32 +08:00
36 lines
591 B
Go
36 lines
591 B
Go
package response
|
|
|
|
import (
|
|
"io"
|
|
"net/http"
|
|
"os"
|
|
"strconv"
|
|
)
|
|
|
|
type File struct {
|
|
Filename string
|
|
}
|
|
|
|
//JSON response
|
|
func (f File) Write(w http.ResponseWriter) error {
|
|
file, err := os.Open(f.Filename)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
fileStat, err := file.Stat()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
w.Header().Set("Content-Disposition", "attachment; filename="+fileStat.Name())
|
|
w.Header().Set("Content-Type", "application/x-asciicast")
|
|
w.Header().Set("Content-Length", strconv.FormatInt(fileStat.Size(), 10))
|
|
|
|
_, err = io.Copy(w, file)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|