[go-nuts] Re: Serve large files
A "defer streamPDFbytes.Close()" is missing - you'll run out of file descriptors... -- 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.
[go-nuts] Re: Serve large files
Hello, I'd recommend you take a look at: https://godoc.org/net/http#ServeFile This also gives you support for range requests and other goodies. Cheers, Christian -- 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.
Re: [go-nuts] Re: Serve large files
Try using os.Open to get a *File (which implements io.Reader), and then > io.Copy to the response from the file. > You should use https://golang.org/pkg/net/http/#ServeFile that's what it does. It will also handle requests with range headers for allowing the client to fetch just a part of the file. -- 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.
[go-nuts] Re: Serve large files
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 golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.