go - golang POST image and text field with multipart/form-data -
yes, have found answers:
http://matt.aimonetti.net/posts/2013/07/01/golang-multipart-file-upload-example/
golang post data using content-type multipart/form-data
http-post file multipart programming in go language
and answers not helpful, because i'm error multipart.newwriter. version of go lang 1.3.3
i'm trying send text data , image form(templates/form.html)
<form enctype="multipart/form-data" method="post"> <div class="row"> <div class="large-6 columns"> <label>image <input type="file" name="image" class="button"/> </label> </div> </div> <div class="row"> <div class="large-12 columns"> <label>about me <textarea type="text" name="aboutmyself" class="aboutmyself"></textarea> </label> </div> </div> </form>
and go approach this:
package main import ( "fmt" "html/template" "io" "log" "net/http" "net/textproto" "os" "reflect" "time" "github.com/gorilla/mux" "github.com/gorilla/schema" ) func render(w http.responsewriter, tmpl string, context map[string]interface{}) { tmpl_list := []string{fmt.sprintf("templates/%s.html", tmpl)} t, err := template.parsefiles(tmpl_list...) if err != nil { log.print("template parsing error: ", err) } err = t.execute(w, context) if err != nil { log.print("template executing error: ", err) } } type fileheader struct { filename string header textproto.mimeheader // contains filtered or unexported fields } func uploadimage(q *http.request, nameinform string) { q.parsemultipartform(32 << 20) file, _, err := q.formfile(nameinform) if err != nil { fmt.println(err) } defer file.close() f, err := os.openfile("./static/uploadimages/"+handler.filename+".jpg", os.o_wronly|os.o_create, 0666) if err != nil { fmt.println(err) } defer f.close() io.copy(f, file) } func main() { rtr := mux.newrouter() //admin section rtr.handlefunc("/myself", myself).methods("get") rtr.handlefunc("/myself", myselfp).methods("post") rtr.pathprefix("/").handler(http.fileserver(http.dir("./static/"))) http.handle("/", rtr) log.println("listening...") http.listenandserve(":3000", nil) } func myself(w http.responsewriter, q *http.request) { render(w,"form",nil) } func myselfp(w http.responsewriter, q *http.request) { err := q.parseform() if err != nil { // handle error fmt.println(err) } uploadimage(q, "image") http.redirect(w, q, "/myself", http.statusfound) }
file uploads perfectly, cann't data textarea. i'm trying understand different between simple form fields , multipart form, , how can data fields
thanks , regards
Comments
Post a Comment