GOlang form upload help?

Hi, TIA. I used Ghostwriter to generate code to receive a multipart file upload in GOlang and I created an index.html for the upload form. On the web deployment, it reports that a file upload was successful but I do not see the file in my repl folder. Is that where is should go? Also, when I run the repl in the ide via the run button, console says “Error retrieving file from form data. http: no such file”


Repl link:

The html form:
> <form action="" method="post" enctype="multipart/form-data">
>     <input type="file" name="file" />
>     <input type="submit" value="Upload" />
> </form>
The Go code:
> func main() {
> 	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {    
>     		if r.Method == "POST" {
>     			// parse input, type multipart/form-data
>     			err := r.ParseMultipartForm(10 << 20)
>     			if err != nil {
>     				fmt.Println("Error parsing multipart form data")
>     				log.Println(err)
>     				return
>     			}
>     			// retrieve file from posted form data
>     			file, handler, err := r.FormFile("file")
>     			if err != nil {
>     				fmt.Println("Error retrieving file from form data")
>     				log.Println(err)
>     				return
>     			}
>           log.Println(handler)
>     			defer file.Close()
>     			// Your code to process PNG would go here
>           decoder()
>     			// return success
>     			fmt.Fprintln(w, "File uploaded successfully.")
>     		} else {
>     			http.ServeFile(w, r, "./index.html")
>     		}
>     	})
>     	http.ListenAndServe(":8000", nil)
> }

Hi there @AlpayKasal

I’m not finding in your code snippet, where in your code do you actually save the file?

1 Like

Thanks for taking the time @WindLother , I ran ghostwriter again and it provided similar code which worked. A good learning that Ghostwriter might need a 2 or 3 attempts at getting it right while i learn :slight_smile: thanks.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.