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 }
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 }
go用multipart form上传文件