go - upload file with same format using beego -
file uploading done not same name filename have tried in html file
<html> <title>go upload</title> <body> <form action="http://localhost:8080/receive" method="post" enctype="multipart/form-data"> <label for="file">filename:</label> <input type="file" name="file" id="file"> <input type="submit" name="submit" value="submit"> </form> </body> </html>
and in beego/go receive.go
package main import ( "fmt" "io" "net/http" "os" ) func uploadhandler(w http.responsewriter, r *http.request) { // formfile function takes in post input id file file, header, err := r.formfile("file") if err != nil { fmt.fprintln(w, err) return } defer file.close() out, err := os.create("/home/vijay/desktop/uploadfile") if err != nil { fmt.fprintf(w, "unable create file writing. check write access privilege") return } defer out.close() // write content post file _, err = io.copy(out, file) if err != nil { fmt.fprintln(w, err) } fmt.fprintf(w, "file uploaded : ") fmt.fprintf(w, header.filename) } func main() { http.handlefunc("/", uploadhandler) http.listenandserve(":8080", nil) }
i able achieve uploading file here same format... not same name.. want file uploaded same name file name
you not using beego controller handle upload
package controllers import ( "github.com/astaxie/beego" ) type maincontroller struct { beego.controller } function (this *maincontroller) getfiles() { this.tplnames = "atemplatefile.html" file, header, er := this.getfile("file") // <<this>> controller , <<file>> id of form field if file != nil { // filename filename := header.filename // save server err := this.savetofile("file", somepathonserver) } }
Comments
Post a Comment