1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| func main() { router := gin.Default()
router.GET("/download", func(c *gin.Context) { file, err := os.Open("../../工作簿1.xlsx") if err != nil { log.Println(err) } defer file.Close()
fileHeader := make([]byte, 512) file.Read(fileHeader)
fileStat, _ := file.Stat()
c.Header("Content-Type", http.DetectContentType(fileHeader)) c.Header("Content-Length", strconv.FormatInt(fileStat.Size(), 10)) disposition := "attachment; filename=下载测试.xlsx" c.Header("Content-Disposition", disposition)
c.Writer.Write(fileHeader) for { tmp := make([]byte, 1000) n, err := file.Read(tmp) if err == io.EOF { return } c.Writer.Write(tmp[:n]) time.Sleep(time.Second * 1) } })
router.Run(":8080") }
|