<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>SysAdmin Adventures &#187; content</title>
	<atom:link href="http://blog.tpa.me.uk/tag/content/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.tpa.me.uk</link>
	<description>Tech notes from a Systems Administrator</description>
	<lastBuildDate>Mon, 06 Sep 2010 12:57:38 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Forcing browsers to &#8220;open with..&#8221; or &#8220;save&#8221; attachments</title>
		<link>http://blog.tpa.me.uk/2009/08/30/forcing-browsers-to-open-with-or-save-attachments/</link>
		<comments>http://blog.tpa.me.uk/2009/08/30/forcing-browsers-to-open-with-or-save-attachments/#comments</comments>
		<pubDate>Sun, 30 Aug 2009 20:22:38 +0000</pubDate>
		<dc:creator>Zordrak</dc:creator>
				<category><![CDATA[Apache]]></category>
		<category><![CDATA[content]]></category>
		<category><![CDATA[firefox]]></category>
		<category><![CDATA[force]]></category>
		<category><![CDATA[ie]]></category>
		<category><![CDATA[mime]]></category>
		<category><![CDATA[open]]></category>

		<guid isPermaLink="false">http://blog.tpa.me.uk/?p=33</guid>
		<description><![CDATA[There are times when you want to force web-browsers to handle a file differently to its default action. Specifically for me, I wanted browsers to not open certain text files themselves, but instead pass them to a text editor. Doing this is quite easy, but how is not well-known.
Obviously the immediate choice is to change [...]]]></description>
			<content:encoded><![CDATA[<p>There are times when you want to force web-browsers to handle a file differently to its default action. Specifically for me, I wanted browsers to not open certain text files themselves, but instead pass them to a text editor. Doing this is quite easy, but how is not well-known.</p>
<p>Obviously the immediate choice is to change the mime-type handling of the browser which is possible in Firefox, although not easily &#8211; I&#8217;m not sure if it&#8217;s possible in IE &#8211; but this has to be done manually for every browser. For Firefox you need to either get an extension for modifying the handlers, or you need to find a text file that has been forced into using the Open/Save dialogue (as per solution below) and then use the &#8220;Always perform this action&#8221; checkbox.</p>
<p>I decided to look at how the file is sent from the server as, often, when a txt file is passed from a CGI script, you get the Open/Save dialogue option. Although there are many different ways you can do it, the canonical way is for the server to pass a &#8220;Content-Disposition: attachment&#8221; response header to the browser which will stop it disposing of the file by opening within the browser regardless of the mime-type it sniffs from the file. As ALWAYS, Internet Explorer does not follow this rule and will often ignore the content-disposition header and act based on the mime-type it sniffs from the file regardless and so, to deal with IE you also need to force the mime-type to application/octet-stream.</p>
<p>This needs to be done in the Apache config, however the best way to do it is with .htaccess as it gives you directory-level control on the files you are affecting. Because it is being done with a regular expression being passed to the FilesMatch directive, you can choose to specify a particular file extension to affect as I have below, or you can specify a particular file, or anything else you can normally do with a regexp.</p>
<p>The following is the correct directive for a .htaccess file if you want to match any file with a .txt file extension, matching upper-case, lower-case or a mixture of cases. When matching the regexp, the mime-type is forced to &#8220;application/octet-stream&#8221; and the &#8220;Content-Disposition: attachment&#8221; header is passed.</p>
<div class="codecolorer-container apache geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="apache codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&lt;<span style="color: #000000; font-weight:bold;">FilesMatch</span> <span style="color: #7f007f;">&quot;<span style="color: #000099; font-weight: bold;">\.</span>(?i:txt)$&quot;</span>&gt;<br />
&nbsp; <span style="color: #00007f;">ForceType</span> application/octet-stream<br />
&nbsp; <span style="color: #00007f;">Header</span> set Content-Disposition attachment<br />
&lt;/<span style="color: #000000; font-weight:bold;">FilesMatch</span>&gt;</div></div>
<p>You&#8217;re welcome <img src='http://blog.tpa.me.uk/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.tpa.me.uk/2009/08/30/forcing-browsers-to-open-with-or-save-attachments/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
