-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathupload.go
80 lines (70 loc) · 1.64 KB
/
upload.go
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package api
import (
"encoding/csv"
"encoding/json"
"fmt"
"github.com/donnie4w/go-logger/logger"
"insane/general/base/appconfig"
"insane/utils"
"io"
"os"
"strings"
)
type UploadMessage struct {
Message
}
type UploadResp struct {
FileName string `json:"fileName"`
FileData [][]string `json:"fileData"`
FileRow int `json:"fileRow"`
FileCol int `json:"fileCol"`
}
func (uploadMessage *UploadMessage) Do() {
fileName := ""
fileData := make([][]string, 0)
fileRow := 0
fileCol := 0
req := uploadMessage.Message.Request
// 设置内存大小
req.ParseMultipartForm(32 << 20)
file, handler, err := req.FormFile("file")
if err != nil {
logger.Debug(err)
return
}
defer file.Close()
fileNameSlice := strings.Split(handler.Filename, ".")
suffix := fileNameSlice[len(fileNameSlice)-1]
fileName = fmt.Sprintf("%d.%s", utils.Now(), suffix)
filePath := fmt.Sprintf("%s/%s", appconfig.GetConfig().File.UploadPath, fileName)
f, err := os.OpenFile(filePath, os.O_RDWR|os.O_CREATE, 0666)
if err != nil {
logger.Debug(err)
return
}
defer f.Close()
io.Copy(f, file)
// 读取csv文件内容返回前五行
if suffix == "csv" {
f.Seek(0, 0) // 将文件指针移到开头
csvData := csv.NewReader(f)
record, err := csvData.ReadAll()
if err != nil {
logger.Debug(err)
return
}
fileRow = len(record)
fileCol = len(record[0])
for _, v := range record[:5] {
fileData = append(fileData, v)
}
}
uploadResp := UploadResp{
FileName: fileName,
FileData: fileData,
FileRow: fileRow,
FileCol: fileCol,
}
resp, _ := json.Marshal(uploadResp)
uploadMessage.Message.ResponseWriter.Write(resp)
}