| | |
| | | package test |
| | | |
| | | import ( |
| | | "bytes" |
| | | "fmt" |
| | | "github.com/gin-gonic/gin" |
| | | "go.uber.org/zap" |
| | | "net/http" |
| | | "srm/global" |
| | | "srm/model/common/request" |
| | | "srm/model/common/response" |
| | |
| | | testReq "srm/model/test/request" |
| | | "srm/service" |
| | | "strconv" |
| | | "time" |
| | | ) |
| | | |
| | | type ContractApi struct { |
| | |
| | | //c.Writer.Header().Set("Content-Type", "application/octect-stream") |
| | | //c.Writer.Header().Set("Content-Disposition", "attachment;filename="+contract.FileName) |
| | | //c.Writer.Write(contract.FileContent) |
| | | c.Data(200, "application/octect-stream", contract.FileContent) |
| | | //c.Data(200, "application/octect-stream", contract.FileContent) |
| | | //reader := bytes.NewReader(contract.FileContent) |
| | | //c.DataFromReader(http.StatusOK, int64(len(contract.FileContent)), "application/pdf", reader, map[string]string{"Content-Disposition": fmt.Sprintf("attachment; filename=%s", contract.FileName)}) |
| | | reader := bytes.NewReader(contract.FileContent) |
| | | c.Writer.Header().Set("Content-Type", "application/pdf") |
| | | c.Writer.Header().Set("Content-Disposition", "inline; filename="+contract.FileName) |
| | | http.ServeContent(c.Writer, c.Request, contract.FileName, time.Now(), reader) |
| | | } |
| | | } |
| | | |
| | | // DownloadContract 下载Contract |
| | | // @Tags Contract |
| | | // @Summary 下载Contract |
| | | // @Security ApiKeyAuth |
| | | // @accept application/json |
| | | // @Produce application/json |
| | | // @Param data query test.Contract true "用id查询Contract" |
| | | // @Success 200 {string} string "{"success":true,"data":{},"msg":"下载成功"}" |
| | | // @Router /con/downloadContract [get] |
| | | func (conApi *ContractApi) DownloadContract(c *gin.Context) { |
| | | var con test.Contract |
| | | err := c.ShouldBindQuery(&con) |
| | | if err != nil { |
| | | response.FailWithMessage(err.Error(), c) |
| | | return |
| | | } |
| | | |
| | | id := c.Query("id") |
| | | if id == "" { |
| | | response.FailWithMessage("id不能为空", c) |
| | | return |
| | | } |
| | | |
| | | val64, err := strconv.ParseUint(id, 10, 64) |
| | | if err != nil { |
| | | response.FailWithMessage("id格式错误", c) |
| | | return |
| | | } |
| | | |
| | | // Convert uint64 to uint |
| | | conId := uint(val64) |
| | | |
| | | contract, err := conService.GetContract(conId) |
| | | if err != nil { |
| | | global.GVA_LOG.Error("获取失败!", zap.Error(err)) |
| | | response.FailWithMessage("获取失败", c) |
| | | return |
| | | } else { |
| | | reader := bytes.NewReader(contract.FileContent) |
| | | // 设置必要的头信息 |
| | | c.Header("Content-Description", "File Transfer") |
| | | c.Header("Content-Transfer-Encoding", "binary") |
| | | c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=%s", contract.FileName)) |
| | | c.Header("Content-Type", "application/pdf") |
| | | |
| | | // 将文件作为响应体发送 |
| | | http.ServeContent(c.Writer, c.Request, contract.FileName, time.Now(), reader) |
| | | c.File(contract.FileName) |
| | | } |
| | | } |