Writing a handler for downloading large files
When an HTTP client requests a large file, it is usually not feasible to load all the file data and then send it to the client. Use io.Copy to stream large content to clients.
How to do it...
This is how you can write a handler to download a large file:
- Set the
Content-Typeheader. - Set the
Content-Lengthheader. - Write the file contents using
io.Copy.
These steps are illustrated here:
func DownloadHandler(w http.ResponseWriter, req *http.Request) {
  fileName := req.PathValue("fileName")
  f, err:= os.Open(filepath.Join("/data",fileName))
  if err!=nil {
    http.Error(w,err.Error(),http.StatusNotFound)
    return
  }
  defer f.Close()
  w.Header.Set("Content-Type","application/octet-stream")
  w.Header.Set("Content-Length",  strconv.Itoa...