Golang : Upload/Receive file progress indicator
It is always nice to have a progress bar telling us how long to wait for the upload or download to complete. In this tutorial, we will learn how to display a text based progress indicator for receiving file.
Note : This is a server side program(console/terminal) and not for client side (web browser)
1) Run this code on your local machine.
package main
import (
"fmt"
"io"
"net/http"
"os"
"time"
)
func uploadPage(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(fmt.Sprintf("<html><title>Go upload</title><body><form action='http://localhost:8080/receive' method='post' enctype='multipart/form-data'><label for='file'>Filename:</label><input type='file' name='file' id='file'><input type='submit' value='Upload' ></form></body></html>")))
}
func uploadProgress(w http.ResponseWriter, r *http.Request) {
mr, err := r.MultipartReader()
if err != nil {
fmt.Fprintln(w, err)
return
}
length := r.ContentLength
//ticker := time.Tick(time.Millisecond) // <-- use this in production
ticker := time.Tick(time.Second) // this is for demo purpose with longer delay
for {
var read int64
var p float32
part, err := mr.NextPart()
if err == io.EOF {
fmt.Printf("\nDone!")
break
}
dst, err := os.OpenFile("upload.jpg", os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
return
}
for {
buffer := make([]byte, 100000)
cBytes, err := part.Read(buffer)
if err == io.EOF {
fmt.Printf("\nLast buffer read!")
break
}
read = read + int64(cBytes)
//fmt.Printf("\r read: %v length : %v \n", read, length)
if read > 0 {
p = float32(read*100) / float32(length)
//fmt.Printf("progress: %v \n", p)
<-ticker
fmt.Printf("\rUploading progress %v", p) // for console
dst.Write(buffer[0:cBytes])
} else {
break
}
}
}
}
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", uploadPage)
mux.HandleFunc("/receive", uploadProgress)
http.ListenAndServe(":8080", mux)
}
2 ) open up your browser and point to http://localhost:8080
3) You will see a simple form for you to upload a file. Choose a file and press upload button.
4) At this point, you should be able to see something like this
$ go run receive.go
Uploading progress 17.9386945
and it will keep updating until the upload from browser to the server is completed.
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
+5.2k Golang : Detect face in uploaded photo like GPlus
+7.8k Golang : Apply Histogram Equalization to color images
+10.8k Golang : Determine if time variables have same calendar day
+9.5k Golang : Create matrix with Gonum Matrix package example
+17.7k Golang : When to use public and private identifier(variable) and how to make the identifier public or private?
+22.7k Golang : Find biggest/largest number in array
+8.8k Golang : Test a slice of integers for odd and even numbers
+6.9k Golang : Regular Expression find string example
+15.6k Golang : How to tell if a file is compressed either gzip or zip ?
+12.7k Golang : convert rune to unicode hexadecimal value and back to rune character
+9.5k Golang : Interfacing with PayPal's IPN(Instant Payment Notification) example
+11.7k Python : Convert IPv6 address to decimal and back to IPv6