Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how can i upload file use go json rest? #226

Open
hdfs010 opened this issue Jan 21, 2018 · 0 comments
Open

how can i upload file use go json rest? #226

hdfs010 opened this issue Jan 21, 2018 · 0 comments

Comments

@hdfs010
Copy link

hdfs010 commented Jan 21, 2018

how can i upload file use go json rest? I am just use this code to do success!

func upload(w rest.ResponseWriter, r *rest.Request) {
r.ParseMultipartForm(32 << 20)
file, handler, err := r.FormFile("uploadfile")
if err != nil {
fmt.Println(err)
return
}
defer file.Close()
f, err := os.OpenFile(handler.Filename, os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
fmt.Println(err)
return
}
defer f.Close()
io.Copy(f, file)
fmt.Fprintln(w.(http.ResponseWriter), "upload ok!")
}

func index(w rest.ResponseWriter, r *rest.Request) {
w.(http.ResponseWriter).Header().Set("Content-Type","text/html")
w.(http.ResponseWriter).Write([]byte(tpl))
}

const tpl = `

<title>上传文件</title> `
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant