Domanda

Can an HTTP header value or another technique be used to set the title in Browsers' windows/tabs for a plain-text file?

When sending a file to the browser with Content-Type: text/plain or similar, the title in the browser window/tab is usually the address to the script or file. Adding a <title> tag to the <head> section of HTML will set the title to a name of your choice, but that option is only possible if you're using Content-Type: text/html or similar.

Let's say that my script currently outputs:

Content-Type: text/plain; charset=UTF-8
Content-Disposition: inline; filename=SomeFile.txt

Hello, World!

A browser will interpret this to mean a text (*.txt) file using UTF-8 encoding with a single line saying Hello, World!. It will also know to use "SomeFile.txt" as the file name when saving. But the name in the browser window/tab will be something like "http://some.host.com/myscript.cgi" when I'd prefer either "SomeFile.txt" or "Some File".

I'd like to set the file with something simple like an HTTP header (E.g. Content-Title: Some File or Content-Disposition: inline; title="Some File"; filename=SomeFile.txt).

How should I communicate this title preference to a Browser?

È stato utile?

Soluzione

Firefox does pick up on Content-Disposition’s filename like you want. You can see this by visiting a Wikia image, like https://static.wikia.nocookie.net/metroid/images/d/db/Armcannons.png/revision/latest?cb=20140820011953, where the supplied filename is displayed in the tab title.

Unfortunately, Chrome and Safari both display latest instead, from the last segment of the URL’s path. (Firefox will also do this in the absence of Content-Disposition.) So if you want this to work in every browser, you may need to append a path segment containing the URL-encoded string you want to display to the end of the URL, and then configure your script/application router/.htaccess/whatever to ignore that last part when finding the file. For your example, the URL would look something like:

https://example.com/SomeFile.txt/Some%20File

Research dead ends

  • Link: <>; rel=self; title="Some File" is the most correct option I found, but doesn’t influence browsers.
  • Some ancient Windows documentation mentions a Content-Name header, but the trail goes cold from there — no RFC seems to mention it.
  • MIME and Usenet headers used Subject for this purpose, but no web browser seems to respect that.

Altri suggerimenti

a) Your example of Content-Disposition is broken; the disposition type is mandatory; see http://greenbytes.de/tech/webdav/rfc6266.html#rfc.section.4.1

b) And no, there is no title parameter, nor do I think there is another way to communicate this to the browser.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top