首页 > 代码库 > go 文件上传

go 文件上传

技术分享
 1 func newfileUploadRequest(uri string, form map[string]string, formFileName, path string) error {
 2     file, err := os.Open(path)
 3     if err != nil {
 4         return err
 5     }
 6     defer file.Close()
 7 
 8     body := &bytes.Buffer{}
 9     writer := multipart.NewWriter(body)
10     part, err := writer.CreateFormFile(formFileName, filepath.Base(path))
11     if err != nil {
12         return err
13     }
14     _, err = io.Copy(part, file)
15     if err != nil {
16         return err
17     }
18 
19     for key, val := range form {
20         err = writer.WriteField(key, val)
21         if err != nil {
22             return err
23         }
24     }
25     err = writer.Close()
26     if err != nil {
27         return err
28     }
29     req, err := http.NewRequest("POST", uri, body)
30     req.Header.Add("Content-Type", writer.FormDataContentType())
31     if err != nil {
32         return err
33     }
34     resp, err := http.DefaultClient.Do(req)
35     if err != nil {
36         return err
37     }
38     defer resp.Body.Close()
39     Body, err := ioutil.ReadAll(resp.Body)
40     if err != nil {
41         return err
42     }
43     fmt.Println(string(Body))
44     return nil
45 }
upload(client)
技术分享
 1 package Network
 2 
 3 import (
 4     "SQL/myDB"
 5     "fmt"
 6     "io"
 7     "logs"
 8     "mime/multipart"
 9     "net/http"
10     "os"
11 )
12 
13 const publicFileDir = "./public"
14 const maxIconSize = 20 << 20
15 
16 func alterIcon(w http.ResponseWriter, req *http.Request) {
17     var (
18         code   int         = Success
19         msg    string      = ""
20         result interface{} = nil
21     )
22     defer func() {
23         writeHTTPResult(w, code, msg, result)
24     }()
25     err := req.ParseMultipartForm(32 << 20)
26     if err != nil {
27         code = errParseForm
28         msg = err.Error()
29         return
30     }
31     logs.Print(req.Form)
32     logs.Print(req.MultipartForm)
33     token := req.FormValue("token")
34     code, msg, userID := judgeToken(token)
35     if code != Success {
36         return
37     }
38     icon, _, err := req.FormFile("icon")
39     if statInterface, ok := icon.(Stat); ok {
40         fileInfo, err := statInterface.Stat()
41         if err == nil {
42             if fileInfo.Size() > (maxIconSize) {
43                 code = errParseFileFailed
44                 msg = fmt.Sprint("上传文件不要超过", maxIconSize>>20, "M")
45                 return
46             }
47         } else if sizeInterface, ok := icon.(Size); ok {
48             if sizeInterface.Size() > (maxIconSize) {
49                 code = errParseFileFailed
50                 msg = fmt.Sprint("上传文件不要超过", maxIconSize>>20, "M")
51                 return
52             }
53         }
54     }
55     if err != nil {
56         code = errParseFileFailed
57         msg = err.Error()
58         return
59     }
60     defer icon.Close()
61     code, msg, result = dealAlterIcon(userID, icon)
62     return
63 }
64 func dealAlterIcon(userID string, icon multipart.File) (code int, msg string, result interface{}) {
65     fileName := "/icon/" + userID + ".ico"
66     code, msg = saveFile(fileName, icon, publicFileDir, maxIconSize)
67     if code != Success {
68         return code, msg, nil
69     }
70     err := myDB.ExecSql("update objects set iconURL = $1 where id = $2", fileName, userID)
71     if err != nil {
72         return errDBExecFailed, err.Error(), nil
73     }
74     return Success, "", map[string]string{"IconURL": fileName}
75 }
76 
77 func saveFile(fileName string, file multipart.File, fileDir string, maxSize int64) (code int, msg string) {
78     File, err := os.OpenFile(publicFileDir+fileName, os.O_WRONLY|os.O_CREATE, 0666)
79     if err != nil {
80         code = errWriteFileFailed
81         msg = err.Error()
82         return
83     }
84     defer File.Close()
85     n, err := io.Copy(File, file)
86     if err != nil {
87         code = errWriteFileFailed
88         msg = err.Error()
89         return
90     }
91     if n > (maxSize) {
92         os.Remove(fileDir + fileName)
93         code = errRequestEntityTooLarge
94         msg = fmt.Sprint("上传文件不要超过", maxSize>>20, "M")
95         return
96     }
97     return Success, ""
98 }
dealUpload(server)

go用multipart form上传文件

go 文件上传