Try using os.Open to get a *File (which implements io.Reader), and then 
io.Copy to the response from the file.

On Tuesday, February 28, 2017 at 5:13:20 AM UTC-6, Jeffrey Smith wrote:
>
> I'm trying to server up a file that can be very large so would like to try 
> and stream the content back.
>
> package main
>
> import (
>         "bytes"
>         "fmt"
>         //"io"
>         "io/ioutil"
>         "log"
>         "net/http"
> )
>
> func main() {
>
>         http.HandleFunc("/", serverFile)
>         if err := http.ListenAndServe(":8085", nil); err != nil {
>                 log.Fatal(err)
>         }
>
> }
>
> func serverFile(w http.ResponseWriter, r *http.Request) {
>
>         streamPDFbytes, err := ioutil.ReadFile("./large.txt")
>         if err != nil {
>                 fmt.Println(err)
>                 return
>         }
>
>         b := bytes.NewBuffer(streamPDFbytes)
>
>         w.Header().Set("Content-Disposition", 
> "attachment;filename=large.txt")
>         //io.Copy(w, b)
>         if _, err := b.WriteTo(w); err != nil {
>                 fmt.Fprintf(w, "%s", err)
>         }
> }
>
> I tried using this but I believe ioutil.readFile actually calls 
> ioutil.readAll so sticks the whole content of the file into a byte array. 
> What am i doing wrong?
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to