Package Mainimport ("Encoding/json" "FMT" "Io/ioutil" "net/http" "Net/url" "OS" "Bufio" "io" "Mime/multipart" "NET/SMTP")
Type Addfeaturestruct{subidint' JSON:"SubID"' file []byte' JSON:"file"'}func addtemplateweb (addrstring) { //Create a form file//Createformfile is used to create a form, the first parameter is the field name, the second argument is the file name varfeat addfeature buf:=New(bytes. Buffer) Writer:=multipart. Newwriter (BUF) writer. Writefield ("Sublib","1") Formfile, err:= writer. Createformfile ("file","5.jpg") ifErr! =Nil {fmt. Println ("Create form file failed:%s\n", Err)} //read data from a file, write a formSrcfile, err: = OS. Open ("/home/wei/1.jpg") ifErr! =Nil {fmt. Println ("%open source file failed:s\n", Err)} Defer Srcfile.close () _, Err=io. Copy (Formfile, Srcfile)ifErr! =Nil {fmt. Println ("Write to form file falied:%s\n", Err)} //Send FormContentType: =writer. Formdatacontenttype () writer. Close ()//You must call Close () before sending to write the end lineRe, err:=http. Post (addr, ContentType, buf) fmt. Println (RE)return }
Go send picture file content via HTTP