views:

499

answers:

2

I am returning a stream in some response setting the appropriate content-type header. The behavior i'm looking for is this:

  • If the browser is able to render content of the given content-type then it should display it in the browser window.

  • If the browser doesn't know how to render the content then it should display the saveas dialog where the filename should be one provided in the response.

The problem that if i set the Content-Disposition header with:

"attachment; filename="myfile.txt""

the browser will always display the savesas dialog.

If i don't set the Content-Disposition the filename used in the saveas dialog is the one in the url that doesn't work in my case.

I also tried setting the Content-Disposition to inline but the outcome is the same.

A: 

I'm not sure if that's possible by default. By security concerns, browser doesn't send all content-types it can handle, but just a few Accept-Encoding, which doesn't help a lot in your scenario.

Maybe you can to ask your user what he/she prefer, and store that information in their profile.

Rubens Farias
+4  A: 

The correct way could be:

Content-Disposition: inline; filename="myfile.txt"

but as you have noted, it is not widely supported, so forget it.

But the real point is that inline data doesn't have a filename (apart from its url). So it's normal that web clients use the name from the url, when saving the file.

The simple way around this is to name your urls with the wanted filename, using some sort of url rewriting.

Patonza
Is it `infile` or `inline`?
AJ
yes, sorry, correct syntax is "inline"
Patonza