From dd2c694c124c4a0f5b1d9c5a876337bd424d697f Mon Sep 17 00:00:00 2001 From: yinbangzhong <zhongbangyin@126.com> Date: 星期三, 19 六月 2024 17:46:08 +0800 Subject: [PATCH] download file by attach model --- controllers/audio.go | 26 ++++++++++++++++++++++++-- 1 files changed, 24 insertions(+), 2 deletions(-) diff --git a/controllers/audio.go b/controllers/audio.go index 8f5f108..9ce7cba 100644 --- a/controllers/audio.go +++ b/controllers/audio.go @@ -111,6 +111,9 @@ if err := models.NewAudioSearch().Create(audio); err != nil { util.ResponseFormat(c, code.SaveFail, "涓婁紶澶辫触") return + } else { + util.ResponseFormat(c, code.SaveFail, "涓婁紶鎴愬姛") + return } go func() { var trainInfoNames = []string{audio.LocomotiveNumber, audio.TrainNumber, audio.Station} @@ -290,13 +293,17 @@ filepath := "" if params.Filetype == 1 { filepath = audio.FilePath - c.Header("Content-Type", "audio/mpeg") // 璁剧疆闊抽鏂囦欢绫诲瀷 + c.Header("Content-Type", "audio/mpeg") // 璁剧疆闊抽鏂囦欢绫诲瀷 + c.Header("Content-Disposition", "attachment; filename="+url.PathEscape(audio.Name)) // 鍦ㄦ祻瑙堝櫒涓洿鎺ユ墦寮� } if params.Filetype == 2 { filepath = audio.TxtFilePath //璁剧疆Content-Type涓簍xt鏂囦欢绫诲瀷,閬垮厤涓枃涔辩爜 c.Header("Content-Type", "text/plain; charset=utf-8") c.Header("Content-Transfer-Encoding", "binary") + //鍘绘帀audio.Name涓�.涔嬪悗鐨勫唴瀹癸紝鍙繚鐣欐枃浠跺悕绉� + fileName := strings.Split(audio.Name, ".")[0] + c.Header("Content-Disposition", "attachment; filename="+url.PathEscape(fmt.Sprintf("%s.txt", fileName))) // 鍦ㄦ祻瑙堝櫒涓洿鎺ユ墦寮� } if filepath == "" { util.ResponseFormat(c, code.InternalError, "鏌ヨ澶辫触") @@ -316,7 +323,6 @@ return } - c.Header("Content-Disposition", "inline; filename="+url.PathEscape(audio.Name)) // 鍦ㄦ祻瑙堝櫒涓洿鎺ユ墦寮� c.Header("Content-Length", fmt.Sprint(fileInfo.Size())) if _, err := io.Copy(c.Writer, file); err != nil { @@ -470,3 +476,19 @@ util.ResponseFormat(c, code.UpdateSuccess, resp) } + +// PreLoadPath +// @Tags 闊抽鑷姩鍔犺浇璺緞 +// @Summary 闊抽鑷姩鍔犺浇璺緞 +// @Produce application/json +// @Success 200 {object} util.Response{data=response.PreLoadPathResp} "鎴愬姛" +// @Router /api-sa/v1/audio/preLoadPath [get] +func (slf AudioCtl) PreLoadPath(c *gin.Context) { + //鑾峰彇PRELOAD_PATH鐜鍙橀噺 + preLoadPath := os.Getenv("PRELOAD_PATH") + if len(preLoadPath) == 0 { + preLoadPath = "./preloads" + } + resp := response.PreLoadPathResp{PreLoadPath: preLoadPath} + util.ResponseFormat(c, code.UpdateSuccess, resp) +} -- Gitblit v1.8.0