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 golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to