Upload multiple files with Go
Uploading multiple files to Go server can be done easily as well. All you need to do is to parse the multiform data and loop through the files in the array. In this tutorial, we will use Go and HTML to achieve the following :
A simple HTML file upload form to upload multiple files to a server
Go program on server side to receive and process the uploaded files
Verify the uploaded file location and content on server.
1) HTML file upload form to upload file to server - goupload_multiple.html
<html>
<title>Go upload</title>
<body>
<form action="http://localhost.com:8080/receive_multiple" method="post" enctype="multipart/form-data">
<label for="file">Filenames:</label>
<input type="file" name="multiplefiles" id="multiplefiles" multiple>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
NOTE: The input id multiplefiles
will be handled by the MultipartForm function in the code below.
2) Go program on server side to receive the uploaded file - receive_multiple.go
package main
import (
"fmt"
"io"
"net/http"
"os"
)
func uploadHandler(w http.ResponseWriter, r *http.Request) {
err := r.ParseMultipartForm(200000) // grab the multipart form
if err != nil {
fmt.Fprintln(w, err)
return
}
formdata := r.MultipartForm // ok, no problem so far, read the Form data
//get the *fileheaders
files := formdata.File["multiplefiles"] // grab the filenames
for i, _ := range files { // loop through the files one by one
file, err := files[i].Open()
defer file.Close()
if err != nil {
fmt.Fprintln(w, err)
return
}
out, err := os.Create("/tmp/" + files[i].Filename)
defer out.Close()
if err != nil {
fmt.Fprintf(w, "Unable to create the file for writing. Check your write access privilege")
return
}
_, err = io.Copy(out, file) // file not files[i] !
if err != nil {
fmt.Fprintln(w, err)
return
}
fmt.Fprintf(w, "Files uploaded successfully : ")
fmt.Fprintf(w, files[i].Filename+"\n")
}
}
func main() {
http.HandleFunc("/", uploadHandler)
http.ListenAndServe(":8080", nil)
}
the code above uses the ParseMultipartForm method to parse the submitted form data and MultipartForm method to process the POST multiple files the input data.
run receive_multiple.go at the server
> go run receive_multiple.go
point your browser to goupload_multiple.html
and upload couple of files of your choice. The uploaded files will be copied to the /tmp
directory
3) Verify the uploaded file location and content on server.
See if the file is uploaded to the /tmp
with ls
command and cat
to see the content
References:
See also : Golang : Upload file from web browser to server
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+19.1k Golang : How to count the number of repeated characters in a string?
+6.6k Golang : Muxing with Martini example
+9.3k Javascript : Read/parse JSON data from HTTP response
+7.5k Golang : Getting Echo framework StartAutoTLS to work
+30k Golang : How to redirect to new page with net/http?
+7.2k Gogland : Single File versus Go Application Run Configurations
+16.4k Golang : Get the IPv4 and IPv6 addresses for a specific network interface
+19.6k Golang : Reset or rewind io.Reader or io.Writer
+5.2k Golang : Get S3 or CloudFront object or file information
+17.1k Golang : Get future or past hours, minutes or seconds
+6.7k Golang : Levenshtein distance example
+10.2k Golang : Allow Cross-Origin Resource Sharing request