messageId := r.URL.Query()["id"][0] out, err := os.Create("./upload/" + messageId + ".mp3")
if err != nil { fmt.Fprintf(w, "Unable to create the file for writing. Check your write access privilege") return } i added this portion but i am getting an error while trying to upload a file using curl error in server is :panic serving 10.10.20.55:62253: runtime error: index out of range [0] with length 0 what is the reason?? On Wednesday, 8 October 2014 at 02:14:13 UTC+5:30 Tobias S. wrote: > Sorry, I misunderstood your problem, so my code snippet is probably > useless for you. But I found this: > > http://stackoverflow.com/questions/25804092/golang-upload-file-err-runtime-error-index-out-of-range > The code sample can be fixed easily and should be what you are after. I > guess it is also better to adapt then the example you cited. > > Entefunc webUploadHandler(w http.ResponseWriter, r *http.Request) { > > file, header, err := r.FormFile("file") // the FormFile function takes in > the POST input id file > > if err != nil { > fmt.Fprintln(w, err) > return > } > defer file.Close() > > // My error comes here > > messageId := r.URL.Query()["id"][0] > out, err := os.Create("./upload/" + messageId + ".mp3") > > if err != nil { > fmt.Fprintf(w, "Unable to create the file for writing. Check your > write access privilege") > return > } > defer out.Close() > > // write the content from POST to the file > _, err = io.Copy(out, file) > if err != nil { > fmt.Fprintln(w, err) > } > > fmt.Fprintf(w,"File uploaded successfully : ") > fmt.Fprintf(w, header.Filename) > > }r code here... > > > > On Tuesday, October 7, 2014 3:56:09 PM UTC+2, Budh Ram wrote: > >> Hi Guys, >> >> I am newbie in Go language. >> I am writing a small HttpServer in Go where I want to upload file to this >> server using curl approach as: >> >>> curl --upload-file foo.txt http://localhost:9090/rp.txt >>> >> >> I am getting following server logs: >> https://gist.github.com/budhrg/355126ba4f03d32008e0 >> >> I searched through docs, existing codes but the closest I found is file >> upload using web >> <https://github.com/Blacktremolo/goload/blob/master/main.go#L49>. >> >> Following is my code for HttpServe which I need to update to accept file >> content and save in server: >> >>> package main >>> >>> >>>> import ( >>> >>> "fmt" >>> >>> "net/http" >>> >>> ) >>> >>> >>>> func main() { >>> >>> http.Handle("/", http.HandlerFunc(upload)) >>> >>> http.ListenAndServe("localhost:9090", nil) >>> >>> } >>> >>> >>>> func upload(w http.ResponseWriter, req *http.Request) { >>> >>> fmt.Fprintf(w, "file name is : %s",req.URL.Path[1:]) >>> >>> fmt.Println("method :", req.Method) >>> >>> fmt.Println("FileSize : ", req.ContentLength) >>> >>> fmt.Println("Header : ",req.Header) >>> >>> fmt.Println("Body : ",req.Body) >>> >>> fmt.Println("Post form : ",req.PostForm) >>> >>> } >>> >>> >>> >> Please help >> >> > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/85f0c138-86e0-4d31-8c1b-4c6ef1d161a6n%40googlegroups.com.