This is a creation in Article, where the information may have evolved or changed.
Go upload file to PHP, the first article is missing a request. Header.set ("Content-type", Formcontenttype) wants to help others
Func upimgaction (imgurl string, url string) {path: = Imgurl extraparams: = map[string]string{"param1": "1", " Param2 ":" 2 "," Param3 ":" 3 ",} request, Err: = Newfileuploadrequest (URL, extraparams," Filedata ", path)//filedata file Name if Err! = Nil {log. Error (Err)}//fmt. PRINTLN (Request) RESP, err: = gclient.do (Request) if err! = Nil {log. Error (Err)} else {body: = &bytes. buffer{} _, Err: = body. Readfrom (resp. Body) If err! = Nil {log. Error (Err)} resp. Body.close ()//fmt. Println (resp. StatusCode)//fmt. Println (resp. Header) fmt. Println (body)}}func newfileuploadrequest (URI string, params map[string]string, paramname, path String) (*http. Request, error) {file, err: = OS. Open (PATH) if err! = Nil {return nil, err} defer file. Close () Body: = &bytes. buffer{} Writer: = multipart. Newwriter (body) for key, Val: = Range params {_ = writer. Writefield (Key, Val)} Formcontenttype: = writer. Formdatacontenttype () part, err: = writer. Createformfile (ParamName, filepath. Base (path)) If err! = Nil {return nil, err} _, err = Io. Copy (part, file) Err = writer. Close () if err! = Nil {return nil, err}//fmt. PRINTLN (body) request, err: = http. Newrequest ("POST", Uri, body) request. Header.set ("Content-type", Formcontenttype) return request, err}
Reference website http://matt.aimonetti.net/posts/2013/07/01/golang-multipart-file-upload-example/
Http://stackoverflow.com/questions/20205796/golang-post-data-using-the-content-type-multipart-form-data
- This article is from: Linux Tutorial Network