go 檔案上傳

來源:互聯網
上載者:User

標籤:range   tcl   ==   filename   copy   sem   closed   pre   cts   

 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 err13     }14     _, err = io.Copy(part, file)15     if err != nil {16         return err17     }18 19     for key, val := range form {20         err = writer.WriteField(key, val)21         if err != nil {22             return err23         }24     }25     err = writer.Close()26     if err != nil {27         return err28     }29     req, err := http.NewRequest("POST", uri, body)30     req.Header.Add("Content-Type", writer.FormDataContentType())31     if err != nil {32         return err33     }34     resp, err := http.DefaultClient.Do(req)35     if err != nil {36         return err37     }38     defer resp.Body.Close()39     Body, err := ioutil.ReadAll(resp.Body)40     if err != nil {41         return err42     }43     fmt.Println(string(Body))44     return nil45 }
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 << 2015 16 func alterIcon(w http.ResponseWriter, req *http.Request) {17     var (18         code   int         = Success19         msg    string      = ""20         result interface{} = nil21     )22     defer func() {23         writeHTTPResult(w, code, msg, result)24     }()25     err := req.ParseMultipartForm(32 << 20)26     if err != nil {27         code = errParseForm28         msg = err.Error()29         return30     }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         return37     }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 = errParseFileFailed44                 msg = fmt.Sprint("上傳檔案不要超過", maxIconSize>>20, "M")45                 return46             }47         } else if sizeInterface, ok := icon.(Size); ok {48             if sizeInterface.Size() > (maxIconSize) {49                 code = errParseFileFailed50                 msg = fmt.Sprint("上傳檔案不要超過", maxIconSize>>20, "M")51                 return52             }53         }54     }55     if err != nil {56         code = errParseFileFailed57         msg = err.Error()58         return59     }60     defer icon.Close()61     code, msg, result = dealAlterIcon(userID, icon)62     return63 }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, nil69     }70     err := myDB.ExecSql("update objects set iconURL = $1 where id = $2", fileName, userID)71     if err != nil {72         return errDBExecFailed, err.Error(), nil73     }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 = errWriteFileFailed81         msg = err.Error()82         return83     }84     defer File.Close()85     n, err := io.Copy(File, file)86     if err != nil {87         code = errWriteFileFailed88         msg = err.Error()89         return90     }91     if n > (maxSize) {92         os.Remove(fileDir + fileName)93         code = errRequestEntityTooLarge94         msg = fmt.Sprint("上傳檔案不要超過", maxSize>>20, "M")95         return96     }97     return Success, ""98 }
dealUpload(server)

go用multipart form上傳檔案

go 檔案上傳

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.