[go-nuts] Re: can't get Content-Type and Content-Disposition to force browser to display "file->save..." dialog in the web browser
Hello, I'm facing a very similar problem but I still cannot solve it by your solution. Would you mind help me ? Thank you very much On Friday, 24 June 2016 23:57:27 UTC+9, David Marceau wrote: > > The core problem was: > w.Header().Set("Content-Disposition","attachment;filename=" + > strOutputFileOfJournalctl) > Should actually be: > w.Header().Add("Content-Disposition","attachment;filename=" + > strOutputFileOfJournalctl) > > I preferred to put the above line before the serveFile. > > That's all. Thanks to everyone who dropped by. > > On Friday, June 24, 2016 at 9:44:00 AM UTC-4, David Marceau wrote: >> >> Here is what is in my import. Maybe I should be looking in goji instead >> of net/http? >> > > >> >> import ( >> "fmt" >> "net" >> "time" >> "strconv" >> "strings" >> "os" >> "encoding/json" >> "net/http" >> "crypto/tls" >> "crypto/rand" >> "github.com/gorilla/mux" >> "github.com/goji/httpauth" >> "github.com/zfjagann/golang-ring" >> "github.com/kabukky/httpscerts" >> "reflect" >> "io" >> "io/ioutil" >> "path/filepath" >> "html/template" >> "os/exec" >> ) >> >> On Friday, June 24, 2016 at 9:40:29 AM UTC-4, David Marceau wrote: >>> >>> I tried to repeat the same ordering as the iris infrastructure within >>> the function, but it still behaves not as expected. It does not show the >>> file->save... dialog. It shows the file within the browser as a web page. >>> >>> func blah (w http.ResponseWriter, r *http.Request) { >>> strOutputFileOfJournalctl = "journalctlLog.json" >>> w.Header().Set("Content-Type","application/octet-stream") //forces >>> the save as dialog >>> >>> strSomeStringInJsonFormat := "{ Blah: 'blah value' }" >>> myOutput := []byte(strSomeStringInJsonFormat) >>> >>> //ATTEMPT #1 >>> //w.Write(myOutput) //displays in web browser page >>> //ATTEMPT #4 >>> //w.Header().Set("Content-Disposition","attachment;filename=" + >>> strOutputFileOfJournalctl) >>> >>> >>> >>> //ATTEMPT #3 >>> //w.Header().Add("Content-Length", strconv.Itoa( len(myOutput) ) ) >>> //w.Write(myOutput) //displays in web browser page >>> //w.Header().Set("Content-Disposition","attachment;filename=" + >>> strOutputFileOfJournalctl) >>> >>> >>> >>> //ATTEMPT #2 >>> w.Header().Add("Content-Length", strconv.Itoa(len(myOutput)) ) >>> tmpFile, _ := ioutil.TempFile(os.TempDir(), "OurGeneratedCustomLog") >>> defer os.Remove(tmpFile.Name()) >>> tmpFile.Write(myOutput) >>> tmpFile.Close() >>> http.ServeFile(w, r, tmpFile.Name()) >>> //ATTEMPT #5 >>> w.Header().Set("Content-Disposition","attachment;filename=" + "\"" + >>> strOutputFileOfJournalctl + "\"") >>> } >>> >>> >>> On Friday, June 24, 2016 at 7:06:59 AM UTC-4, David Marceau wrote: Again, I want to clarify the file does arrive in the browser, but I want to ensure the "file->save..." dialog appears in the web browser when it arrives. I found some older code I wrote a couple of years ago that was behaving as expected: w.Header().Set("Content-Type", "application/octet-stream") w.Header().Set("Content-Disposition", "attachment; filename=" + myBasePdf + ".pdf") http.ServeFile(w, req, myGenPdfFileName) I acknowledge when I wrote this email I made a typo, but in my code I do have the Itoa correctly. w.Header().Set("Content-Length", strconv.Itoa( len(myCmdOutput) ) ) I never used that content-length field because I read somewhere that I shouldn't. Last night I took a look at iris to see how they do it and found: https://github.com/kataras/iris/blob/master/context.go#L583 err := ctx.ServeFile(filename, false) if err != nil { return err } ctx.RequestCtx.Response.Header.Set(contentDisposition, " attachment;filename="+destinationName) I am scratching my head since the header set content-disposition is happening after the ServeFile which is different from what all the docs and what I am used to seeing. It seems calling these functions are order-independant. When does the connection actually send the file over the connection? I believe the Iris send file also provides what I want as expected behaviour, but I haven't tried it yet. On Thursday, June 23, 2016 at 6:15:16 PM UTC-4, Val wrote: > > The commented line seems to have typo strconv.Ito > > Maybe the typo prevents proper recompilation, and server goes on with > old code? > -- 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: can't get Content-Type and Content-Disposition to force browser to display "file->save..." dialog in the web browser
That's very strange. Why didn't Set work? Was there some already-existing Content-Disposition value in the header that you needed to retain? On Fri, Jun 24, 2016 at 7:57 AM David Marceauwrote: > The core problem was: > w.Header().Set("Content-Disposition","attachment;filename=" + > strOutputFileOfJournalctl) > Should actually be: > w.Header().Add("Content-Disposition","attachment;filename=" + > strOutputFileOfJournalctl) > > I preferred to put the above line before the serveFile. > > That's all. Thanks to everyone who dropped by. > > > On Friday, June 24, 2016 at 9:44:00 AM UTC-4, David Marceau wrote: >> >> Here is what is in my import. Maybe I should be looking in goji instead >> of net/http? >> > > >> >> import ( >> "fmt" >> "net" >> "time" >> "strconv" >> "strings" >> "os" >> "encoding/json" >> "net/http" >> "crypto/tls" >> "crypto/rand" >> "github.com/gorilla/mux" >> "github.com/goji/httpauth" >> "github.com/zfjagann/golang-ring" >> "github.com/kabukky/httpscerts" >> "reflect" >> "io" >> "io/ioutil" >> "path/filepath" >> "html/template" >> "os/exec" >> ) >> >> On Friday, June 24, 2016 at 9:40:29 AM UTC-4, David Marceau wrote: >>> >>> I tried to repeat the same ordering as the iris infrastructure within >>> the function, but it still behaves not as expected. It does not show the >>> file->save... dialog. It shows the file within the browser as a web page. >>> >>> func blah (w http.ResponseWriter, r *http.Request) { >>> strOutputFileOfJournalctl = "journalctlLog.json" >>> w.Header().Set("Content-Type","application/octet-stream") //forces >>> the save as dialog >>> >>> strSomeStringInJsonFormat := "{ Blah: 'blah value' }" >>> myOutput := []byte(strSomeStringInJsonFormat) >>> >>> //ATTEMPT #1 >>> //w.Write(myOutput) //displays in web browser page >>> //ATTEMPT #4 >>> //w.Header().Set("Content-Disposition","attachment;filename=" + >>> strOutputFileOfJournalctl) >>> >>> >>> >>> //ATTEMPT #3 >>> //w.Header().Add("Content-Length", strconv.Itoa( len(myOutput) ) ) >>> //w.Write(myOutput) //displays in web browser page >>> //w.Header().Set("Content-Disposition","attachment;filename=" + >>> strOutputFileOfJournalctl) >>> >>> >>> >>> //ATTEMPT #2 >>> w.Header().Add("Content-Length", strconv.Itoa(len(myOutput)) ) >>> tmpFile, _ := ioutil.TempFile(os.TempDir(), "OurGeneratedCustomLog") >>> defer os.Remove(tmpFile.Name()) >>> tmpFile.Write(myOutput) >>> tmpFile.Close() >>> http.ServeFile(w, r, tmpFile.Name()) >>> //ATTEMPT #5 >>> w.Header().Set("Content-Disposition","attachment;filename=" + "\"" + >>> strOutputFileOfJournalctl + "\"") >>> } >>> >>> >>> On Friday, June 24, 2016 at 7:06:59 AM UTC-4, David Marceau wrote: Again, I want to clarify the file does arrive in the browser, but I want to ensure the "file->save..." dialog appears in the web browser when it arrives. I found some older code I wrote a couple of years ago that was behaving as expected: w.Header().Set("Content-Type", "application/octet-stream") w.Header().Set("Content-Disposition", "attachment; filename=" + myBasePdf + ".pdf") http.ServeFile(w, req, myGenPdfFileName) I acknowledge when I wrote this email I made a typo, but in my code I do have the Itoa correctly. w.Header().Set("Content-Length", strconv.Itoa( len(myCmdOutput) ) ) I never used that content-length field because I read somewhere that I shouldn't. Last night I took a look at iris to see how they do it and found: https://github.com/kataras/iris/blob/master/context.go#L583 err := ctx.ServeFile(filename, false) if err != nil { return err } ctx.RequestCtx.Response.Header.Set(contentDisposition, " attachment;filename="+destinationName) I am scratching my head since the header set content-disposition is happening after the ServeFile which is different from what all the docs and what I am used to seeing. It seems calling these functions are order-independant. When does the connection actually send the file over the connection? I believe the Iris send file also provides what I want as expected behaviour, but I haven't tried it yet. On Thursday, June 23, 2016 at 6:15:16 PM UTC-4, Val wrote: > > The commented line seems to have typo strconv.Ito > > Maybe the typo prevents proper recompilation, and server goes on with > old code? > -- > 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. > -- You received this message because you are subscribed
[go-nuts] Re: can't get Content-Type and Content-Disposition to force browser to display "file->save..." dialog in the web browser
The core problem was: w.Header().Set("Content-Disposition","attachment;filename=" + strOutputFileOfJournalctl) Should actually be: w.Header().Add("Content-Disposition","attachment;filename=" + strOutputFileOfJournalctl) I preferred to put the above line before the serveFile. That's all. Thanks to everyone who dropped by. On Friday, June 24, 2016 at 9:44:00 AM UTC-4, David Marceau wrote: > > Here is what is in my import. Maybe I should be looking in goji instead > of net/http? > > > import ( > "fmt" > "net" > "time" > "strconv" > "strings" > "os" > "encoding/json" > "net/http" > "crypto/tls" > "crypto/rand" > "github.com/gorilla/mux" > "github.com/goji/httpauth" > "github.com/zfjagann/golang-ring" > "github.com/kabukky/httpscerts" > "reflect" > "io" > "io/ioutil" > "path/filepath" > "html/template" > "os/exec" > ) > > On Friday, June 24, 2016 at 9:40:29 AM UTC-4, David Marceau wrote: >> >> I tried to repeat the same ordering as the iris infrastructure within the >> function, but it still behaves not as expected. It does not show the >> file->save... dialog. It shows the file within the browser as a web page. >> >> func blah (w http.ResponseWriter, r *http.Request) { >> strOutputFileOfJournalctl = "journalctlLog.json" >> w.Header().Set("Content-Type","application/octet-stream") //forces >> the save as dialog >> >> strSomeStringInJsonFormat := "{ Blah: 'blah value' }" >> myOutput := []byte(strSomeStringInJsonFormat) >> >> //ATTEMPT #1 >> //w.Write(myOutput) //displays in web browser page >> //ATTEMPT #4 >> //w.Header().Set("Content-Disposition","attachment;filename=" + >> strOutputFileOfJournalctl) >> >> >> >> //ATTEMPT #3 >> //w.Header().Add("Content-Length", strconv.Itoa( len(myOutput) ) ) >> //w.Write(myOutput) //displays in web browser page >> //w.Header().Set("Content-Disposition","attachment;filename=" + >> strOutputFileOfJournalctl) >> >> >> >> //ATTEMPT #2 >> w.Header().Add("Content-Length", strconv.Itoa(len(myOutput)) ) >> tmpFile, _ := ioutil.TempFile(os.TempDir(), "OurGeneratedCustomLog") >> defer os.Remove(tmpFile.Name()) >> tmpFile.Write(myOutput) >> tmpFile.Close() >> http.ServeFile(w, r, tmpFile.Name()) >> //ATTEMPT #5 >> w.Header().Set("Content-Disposition","attachment;filename=" + "\"" + >> strOutputFileOfJournalctl + "\"") >> } >> >> >> On Friday, June 24, 2016 at 7:06:59 AM UTC-4, David Marceau wrote: >>> >>> Again, I want to clarify the file does arrive in the browser, but I want >>> to ensure the "file->save..." dialog appears in the web browser when it >>> arrives. I found some older code I wrote a couple of years ago that was >>> behaving as expected: >>> w.Header().Set("Content-Type", "application/octet-stream") >>> w.Header().Set("Content-Disposition", "attachment; filename=" + >>> myBasePdf + ".pdf") >>> http.ServeFile(w, req, myGenPdfFileName) >>> >>> >>> I acknowledge when I wrote this email I made a typo, but in my code I do >>> have the Itoa correctly. >>> w.Header().Set("Content-Length", strconv.Itoa( len(myCmdOutput) ) ) >>> I never used that content-length field because I read somewhere that I >>> shouldn't. >>> >>> Last night I took a look at iris to see how they do it and found: >>> https://github.com/kataras/iris/blob/master/context.go#L583 >>> err := ctx.ServeFile(filename, false) >>> if err != nil { >>> return err >>> } >>> >>> ctx.RequestCtx.Response.Header.Set(contentDisposition, " >>> attachment;filename="+destinationName) >>> >>> I am scratching my head since the header set content-disposition is >>> happening after the ServeFile which is different from what all the docs and >>> what I am used to seeing. It seems calling these functions are >>> order-independant. When does the connection actually send the file over >>> the connection? >>> >>> I believe the Iris send file also provides what I want as expected >>> behaviour, but I haven't tried it yet. >>> >>> On Thursday, June 23, 2016 at 6:15:16 PM UTC-4, Val wrote: The commented line seems to have typo strconv.Ito Maybe the typo prevents proper recompilation, and server goes on with old code? >>> -- 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.