In this blog I will be using the native Golang http package to upload files, no third party packages. We will see how to upload single and multiple files.
The HTML form
The form is simple and straight forward. The only thing to note is the enctype="multipart/form-data"
attribute. This is required for uploading files.
Single file upload
💡Note: All of the _
underscore variables are errors, I just ignored them for the sake of simplicity. so don't do that in production.
As you noticed we get the uploaded file with the r.FormFile("input_name_here")
function, it returns 3 variables, file
which is the uploaded file itself, header
which contains information about the file like the name, size, MIME type etc..., you can use these information to validate the file before uploading it, and finally _
which is the error if any.
Multiple files upload
First of all before you can select multiple files, you need to add the multiple
attribute to the file input
Just FYI, the r.MultipartForm.File
is a map[string][]*multipart.FileHeader
where the key is the name of the input in the form. The file
variable contains the same information as the header
variable in the single file upload example, (size, name, MIME type etc...)
The other fields
To get the other fields, there are two options, which at some point you will need to use both of them.
You can use the first option when you want to get a single value, and the second option when you want to get multiple values, in our case the checkbox can have multiple values.
So r.FormValue("name")
is like doing r.MultipartForm.Value["name"][0]
.
That's it, I hope you enjoyed this tutorial, if you have any questions or suggestions, you can find all of this on GitHub.