From c40d07e760598c0aae4353f7a1e2d6b01747e83f Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期三, 12 六月 2024 11:54:54 +0800
Subject: [PATCH] 文件下载&状态筛选

---
 request/audio.go     |   12 ++++++------
 controllers/audio.go |   27 +++++++++++++++++++++++----
 2 files changed, 29 insertions(+), 10 deletions(-)

diff --git a/controllers/audio.go b/controllers/audio.go
index 5f9d8e5..7c58dd9 100644
--- a/controllers/audio.go
+++ b/controllers/audio.go
@@ -2,8 +2,10 @@
 
 import (
 	"errors"
+	"fmt"
 	"github.com/gin-gonic/gin"
 	"gorm.io/gorm"
+	"io"
 	"os"
 	"path"
 	"speechAnalysis/constvar"
@@ -268,10 +270,27 @@
 		return
 	}
 
-	c.Header("Content-Description", "File Transfer")
-	c.Header("Content-Disposition", "attachment; filename="+audio.Name)
-	c.Header("Content-Type", "application/octet-stream")
-	c.File(audio.FilePath)
+	file, err := os.Open(audio.FilePath)
+	if err != nil {
+		util.ResponseFormat(c, code.InternalError, "鏂囦欢鎵撳紑澶辫触")
+		return
+	}
+	defer file.Close()
+
+	fileInfo, err := file.Stat()
+	if err != nil {
+		util.ResponseFormat(c, code.InternalError, "鑾峰彇鏂囦欢淇℃伅澶辫触")
+		return
+	}
+
+	c.Header("Content-Disposition", "inline; filename="+audio.Name) // 鍦ㄦ祻瑙堝櫒涓洿鎺ユ墦寮�
+	c.Header("Content-Length", fmt.Sprint(fileInfo.Size()))
+	c.Header("Content-Type", "audio/mpeg") // 璁剧疆闊抽鏂囦欢绫诲瀷
+
+	if _, err := io.Copy(c.Writer, file); err != nil {
+		util.ResponseFormat(c, code.InternalError, "鏂囦欢浼犺緭澶辫触")
+		return
+	}
 }
 
 // BatchProcess
diff --git a/request/audio.go b/request/audio.go
index 8cedbe1..1fe4ef9 100644
--- a/request/audio.go
+++ b/request/audio.go
@@ -7,15 +7,15 @@
 
 type GetAudioList struct {
 	PageInfo
-	Keyword          string                 `form:"keyword"`                                                                            // 鍏抽敭瀛�
+	Keyword          string                 `form:"keyword"`                                                                         // 鍏抽敭瀛�
 	LocomotiveNumber string                 `gorm:"index;type:varchar(255);not null;default:'';comment:鏈鸿溅鍙�" form:"locomotiveNumber"` // 鏈鸿溅鍙�
-	TrainNumber      string                 `gorm:"index;type:varchar(255);not null;default:'';comment:杞︽" form:"trainNumber"`        // 杞︽
+	TrainNumber      string                 `gorm:"index;type:varchar(255);not null;default:'';comment:杞︽" form:"trainNumber"`       // 杞︽
 	DriverNumber     string                 `gorm:"index;type:varchar(255);not null;default:'';comment:鍙告満鍙�" form:"driverNumber"`     // 鍙告満鍙�
 	StationNumber    string                 `gorm:"index;type:varchar(255);not null;default:'';comment:杞︾珯鍙�" form:"stationNumber"`    // 杞︾珯鍙�
-	BeginTime        time.Time              `form:"beginTime"`                                                                          //寮�濮嬫椂闂�
-	EndTime          time.Time              `form:"endTime"`                                                                            //缁撴潫鏃堕棿
-	IsFollowed       constvar.BoolType      `form:"isFollowed"`                                                                         //鏄惁鍏虫敞 1鍏虫敞 2鏈叧娉�
-	StatusList       []constvar.AudioStatus `form:"statusList"`                                                                         //闊抽鐘舵�佹暟缁�
+	BeginTime        time.Time              `form:"beginTime"`                                                                       //寮�濮嬫椂闂�
+	EndTime          time.Time              `form:"endTime"`                                                                         //缁撴潫鏃堕棿
+	IsFollowed       constvar.BoolType      `form:"isFollowed"`                                                                      //鏄惁鍏虫敞 1鍏虫敞 2鏈叧娉�
+	StatusList       []constvar.AudioStatus `form:"statusList"`                                                                      //闊抽鐘舵�佹暟缁�
 }
 
 type ProcessAudio struct {

--
Gitblit v1.8.0