博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
go 文件上传
阅读量:5263 次
发布时间:2019-06-14

本文共 4203 字,大约阅读时间需要 14 分钟。

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上传文件

转载于:https://www.cnblogs.com/cdyboke/p/6816357.html

你可能感兴趣的文章
linux下WPS的使用
查看>>
Web Api 利用 cors 实现跨域
查看>>
hdu 3938 并查集
查看>>
instanceof
查看>>
《深入分析Java Web技术内幕》读书笔记之JVM内存管理
查看>>
python之GIL release (I/O open(file) socket time.sleep)
查看>>
2015/8/4 告别飞思卡尔,抛下包袱上路
查看>>
软件开发与模型
查看>>
161017、SQL必备知识点
查看>>
kill新号专题
查看>>
MVC学习系列——Model验证扩展
查看>>
C# GC 垃圾回收机制
查看>>
mysqladmin 修改和 初始化密码
查看>>
字符串
查看>>
vue2.x directive - 限制input只能输入正整数
查看>>
实现MyLinkedList类深入理解LinkedList
查看>>
自定义返回模型
查看>>
C#.NET 大型通用信息化系统集成快速开发平台 4.1 版本 - 客户端多网络支持
查看>>
HDU 4122
查看>>
Suite3.4.7和Keil u3自带fx2.h、fx2regs.h文件的异同
查看>>