问题描述
我遇到访问文件的问题,我上传w / golang。我真的是新的语言,并经历了一些尝试 - 无法找到任何答案,这种在线。
我做错了什么?在这个代码中,我从来没有去到它列出上传文件的块。
func处理程序(w http.ResponseWriter ,r * http.Request){
fmt.Println(handling req ...)
if r.Method ==GET{
fmt.Println GET请求...)
} else {
//解析多部分的东西,如果有
err:= r.ParseMultipartForm(15485760)
if err == nil {
form:= r.MultipartForm
if form == nil {
fmt.Println(no files。 ..)
} else {
defer form.RemoveAll()
//我从来没有看到这真的发生
fmt.Printf(%d files ,len(form.File))
}
} else {
http.Error(w,err.Error(),http.StatusInternalServerError)
fmt.Println(err。错误())
}
}
//fmt.Fprintf(w,你好,我爱%s!,r.URL.Path [1:] )
fmt.Println(离开...)
}
更新
我能够得到上面的代码工作。这是伟大的。下面的答案显示了如何做到这一点async,这可能是一个比我的更好的代码示例。
/ strong> 下载最新的golang版本。
我以前遇到过这个问题,使用旧的golang版本,我不知道发生了什么,但最新的golang工作。 =)
我的上传处理程序代码如下:
完整代码:
//解析请求
const _24K =(1 if err = req.ParseMultipartForm(_24K); nil!= err {
status = http.StatusInternalServerError
return
}
for _,fheaders:= range req.MultipartForm.File {
for _,hdr: =范围fheaders {
//打开上传
var infile multipart.File
if infile,err = hdr.Open(); nil!= err {
status = http.StatusInternalServerError
return
}
//打开目标
var outfile * os.File
如果outfile,err = os.Create(./ uploaded /+ hdr.Filename); nil!= err {
status = http.StatusInternalServerError
return
}
// 32K缓冲区拷贝
var写入int64 $ b $如果写入,err = io复制(outfile,infile); nil!= err {
status = http.StatusInternalServerError
return
}
res.Write([] byte(uploaded file:+ hdr.Filename +; length: + strconv.Itoa(int(written))))
}
}
I'm having issues with accessing files i upload w/ golang. I'm really new to the language and have gone through more than a few attempts-- can't find any answers to this online either.
What am i doing wrong? In this code, i never get to the block where it lists the # of files uploaded.
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Println("handling req...")
if r.Method =="GET"{
fmt.Println("GET req...")
} else {
//parse the multipart stuff if there
err := r.ParseMultipartForm(15485760)
//
if err == nil{
form:=r.MultipartForm
if form==nil {
fmt.Println("no files...")
} else {
defer form.RemoveAll()
// i never see this actually occur
fmt.Printf("%d files",len(form.File))
}
} else {
http.Error(w,err.Error(),http.StatusInternalServerError)
fmt.Println(err.Error())
}
}
//fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])
fmt.Println("leaving...")
}
Update
I was able to get the above code to work. Which is great. The answer below shows how to do it async, which may be a better code sample than mine.
Answer Download the latest golang release.
I experienced the problem before, using the old golang versions, I do not know what happened, but with the latest golang its working. =)
My upload handler code below...Full code at: http://noypi-linux.blogspot.com/2014/07/golang-web-server-basic-operatons-using.html
// parse request
const _24K = (1 << 10) * 24
if err = req.ParseMultipartForm(_24K); nil != err {
status = http.StatusInternalServerError
return
}
for _, fheaders := range req.MultipartForm.File {
for _, hdr := range fheaders {
// open uploaded
var infile multipart.File
if infile, err = hdr.Open(); nil != err {
status = http.StatusInternalServerError
return
}
// open destination
var outfile *os.File
if outfile, err = os.Create("./uploaded/" + hdr.Filename); nil != err {
status = http.StatusInternalServerError
return
}
// 32K buffer copy
var written int64
if written, err = io.Copy(outfile, infile); nil != err {
status = http.StatusInternalServerError
return
}
res.Write([]byte("uploaded file:" + hdr.Filename + ";length:" + strconv.Itoa(int(written))))
}
}
这篇关于在Golang中访问已上传的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!