I'm attempting to serve PDF files from a Go app, however I am getting an unknown "network error" response half way through the download. This happens when attempting to download on both Chrome & Firefox but not if I use wget from a remote server to download the file.
It must be some way that net/http is attempting to serve the file. They're PDFs, all cached, so the PDF itself is a slice of bytes currently in memory. The problem is not the slice of bytes that is being sent, those are all valid and complete PDF files.
For some downloads it works correctly, and for others the download cuts off with a "Network Error" response from Chrome. You can see an example of it doing this here:
Following is the code for the download:
func DownloadPDF(rw http.ResponseWriter, r *http.Request, fid int) {
book := engine.BookInfo[fid]
data, err := bookcache.PDF(int(fid))
if err != nil {
panic(err)
}
rw.Header().Set(`Content-Type`, `application/pdf`)
rw.Header().Set(`Filename`, book.LinkTitle + `.pdf`)
rw.Header().Set(`Content-Disposition`, `attachment`)
rw.Header().Set(`Content-Transfer-Encoding`, `binary`)
rw.Header().Set(`Accept-Ranges`, `bytes`)
rw.Header().Set(`Connection`, `close`)
rw.Header().Set(`Content-Length`, conv.String(len(data)))
// Send
var i int
to := len(data) - 65536
for ; i<to; i+=65536 {
rw.Write(data[i:i+65536])
}
rw.Write(data[i:])
}
Does anyone have an idea as to why this could be happening and how to fix it?'
Thanks,
Alasdair