调整UDM用户函数导出CSV文件
This commit is contained in:
43
lib/core/file/csv.go
Normal file
43
lib/core/file/csv.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package file
|
||||
|
||||
import (
|
||||
"encoding/csv"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"ems.agt/lib/log"
|
||||
)
|
||||
|
||||
// 写入CSV文件,需要转换数据
|
||||
// 例如:
|
||||
// data := [][]string{}
|
||||
// data = append(data, []string{"姓名", "年龄", "城市"})
|
||||
// data = append(data, []string{"1", "2", "3"})
|
||||
// err := file.WriterCSVFile(data, filePath)
|
||||
func WriterCSVFile(data [][]string, filePath string) error {
|
||||
// 获取文件所在的目录路径
|
||||
dirPath := filepath.Dir(filePath)
|
||||
|
||||
// 确保文件夹路径存在
|
||||
err := os.MkdirAll(dirPath, os.ModePerm)
|
||||
if err != nil {
|
||||
log.Errorf("创建文件夹失败 CreateFile %v", err)
|
||||
}
|
||||
|
||||
// 创建或打开文件
|
||||
file, err := os.Create(filePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
// 创建CSV编写器
|
||||
writer := csv.NewWriter(file)
|
||||
defer writer.Flush()
|
||||
|
||||
// 写入数据
|
||||
for _, row := range data {
|
||||
writer.Write(row)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
"ems.agt/lib/core/vo"
|
||||
"github.com/gorilla/mux"
|
||||
@@ -59,6 +60,13 @@ func JSON(w http.ResponseWriter, code int, data any) {
|
||||
}
|
||||
}
|
||||
|
||||
// 将文件导出到外部下载
|
||||
func FileAttachment(w http.ResponseWriter, r *http.Request, filepath, filename string) {
|
||||
w.Header().Set("Content-Disposition", `attachment; filename=`+url.QueryEscape(filename))
|
||||
w.Header().Set("Content-Type", "application/octet-stream")
|
||||
http.ServeFile(w, r, filepath)
|
||||
}
|
||||
|
||||
/// ==== 登录用户信息, 通过中间件后预置入
|
||||
|
||||
// 定义自定义类型作为键
|
||||
|
||||
Reference in New Issue
Block a user