title: Special Characters in Content-Disposition filename
body: <p>My question is a duplicate of <a href="https://stackoverflow.com/questions/93551/">How to encode the filename parameter of Content-Disposition header in HTTP?</a>
  But since that question was asked a long time ago and there is still no satisfying answer (in my opinion), I would like to ask again.</p>
  
  <p>I develop a C++ CGI application that delivers files that can contain special characters in their names like<br> "<b>weird # € = { } ; filename.txt</b>"</p>
  
  <p>There seems to be no possibility to set the HTTP Content-Dispostion in a way that it works for every browser like</p>
  
  <ul>
  <li>Internet Explorer</li>
  <li>Firefox</li>
  <li>Chrome</li>
  <li>Opera</li>
  <li>Safari</li>
  </ul>
  
  <p>I would be happy with a different solution for every browser. <br>
  Now that is how far I came:</p>
  
  <p><b>Internet Explorer</b> (added double quotes and replaced # and ; ) </p>
  
  <pre><code>Content-Disposition: attachment; filename="weird %23 € = { } %3B filename.txt"
  </code></pre>
  
  <p><b>Firefox</b> (double quotes seem to work. nothing more to do):</p>
  
  <pre><code>Content-Disposition: attachment; filename="weird # € = { } ; filename.txt"
  </code></pre>
  
  <p>Another working alternative:</p>
  
  <pre><code>Content-Disposition: attachment; filename*=UTF-8''weird%20%23%20%e2%82%ac%20%3D%20%7B%20%7D%20%3B%20filename.txt
  </code></pre>
  
  <p><b>Chrome</b></p>
  
  <p>when using only double quotes these problems arise:</p>
  
  <ul>
  <li>= disapears in filenames </li>
  <li>€ will be replaced by - <br></li>
  </ul>
  
  <p>but this works:</p>
  
  <pre><code>Content-Disposition: attachment; filename*=UTF-8''weird%20%23%20%e2%82%ac%20%3D%20%7B%20%7D%20%3B%20filename.txt
  </code></pre>
  
  <p><b>Opera</b></p>
  
  <p>Using duoble quotes or using the syntax: filename*=UTF-8''... produces the following problems:</p>
  
  <ul>
  <li>Multiple sticked together spaces in filenames are reduced to one</li>
  <li>{ and } disapear: "<b>ab{}cd.txt</b>" -> "<b>abcd.txt</b>"</li>
  <li>filenames get cut off after ; in it: "<b>abc ; def.txt</b>" -> "<b>abc</b>"</li>
  </ul>
  
  <p><b>EDIT 2:</b> This was because of filename length limitations. This syntax works with Opera:</p>
  
  <pre><code>Content-Disposition: attachment; filename*=UTF-8''weird%20%23%20%e2%82%ac%20%3D%20%7B%20%7D%20%3B%20filename.txt
  </code></pre>
  
  <p><b>Safari</b></p>
  
  <ul>
  <li><p>€ will be replaced by an invisble character (using double quotes)</p>
  
  <pre><code>no solution that prevents that little problem
  </code></pre></li>
  </ul>
  
  <p>The suggestion from the other thread (mentioned above) using</p>
  
  <pre><code>Content-Disposition: attachment; filename*=UTF-8''weird%20%23%20%80%20%3D%20%7B%20%7D%20%3B%20filename.txt
  </code></pre>
  
  <p>didn't work for me. The escape characters won't be translated back or the browser wants to save to file with the name of my cgi application. That was because my encoding was wrong. I did not encode according to RFC 5987. But Safari isn't using this encoding anyway. So no solution for the € character so far.</p>
  
  <p>BTW: An UTF-8 converter <a href="http://www.rishida.net/tools/conversion/" rel="nofollow noreferrer">http://www.rishida.net/tools/conversion/</a></p>
  
  <p>I used the latest version of every browser fo these tests:</p>
  
  <ul>
  <li>Firefox 7</li>
  <li>Internet Explorer 9</li>
  <li>Chrome 15</li>
  <li>Opera 11.5</li>
  <li>Safari 5.1</li>
  </ul>
  
  <p>PS: I tried all special characters on my keyboard. I used in this thread only the ones that made trouble.</p>
  
  <h2>EDIT:</h2>
  
  <p>I also tried a filename with all special characters on my keyboard (that are possible in a filename) and that did not work as it did with the test string above:</p>
  
  <p>Complete Test string:</p>
  
  <pre><code>0 ! § $ % &amp; ( ) = ` ´ { }    [ ] ² ³ @ € µ ^ ° ~ + ' # - _ . , ; ü ä ö ß 9.jpg
  </code></pre>
  
  <p>Encoded Test String:</p>
  
  <pre><code>0%20%21%20%C2%A7%20%24%20%25%20%26%20%28%20%29%20%3D%20%60%20%C2%B4%20%7B%20%7D%20%20%20%20%5B%20%5D%20%C2%B2%20%C2%B3%20%40%20%E2%82%AC%20%C2%B5%20%5E%20%C2%B0%20~%20%2B%20%27%20%23%20-%20_%20.%20%2C%20%3B%20%C3%BC%20%C3%A4%20%C3%B6%20%C3%9F%209.jpg
  </code></pre>
  
  <p>Using this method:</p>
  
  <pre><code>Content-Disposition: attachment; filename*=UTF-8''0%20%21%20%C2%A7%20%24%20%25%20%26%20%28%20%29%20%3D%20%60%20%C2%B4%20%7B%20%7D%20%20%20%20%5B%20%5D%20%C2%B2%20%C2%B3%20%40%20%E2%82%AC%20%C2%B5%20%5E%20%C2%B0%20~%20%2B%20%27%20%23%20-%20_%20.%20%2C%20%3B%20%C3%BC%20%C3%A4%20%C3%B6%20%C3%9F%209.jpg
  </code></pre>
  
  <p>I had the following results:</p>
  
  <ul>
  <li>Firefox works</li>
  <li>Chrome works</li>
  <li>IE:  $ % &amp; ( ) = ` ´ { }    [ ] ² ³ @ € µ ^ ° ~ + ' # - _ . , ; ü ä ö ß 9.jpg (removed the first 6 characters). <b>EDIT 2:</b> This was because of filename length limitations of the browser. It startet to cut off the filename from the start of the string. I didn't go deep into this but it looks like normal filenames can be about 200 characters long and filenames with many escape sequesnces even more but less than 250. But that's OK.</li>
  <li>Opera: 0 ! § $ % &amp; ( ) = ` ´ [ ] ² ³ @ € µ ^ ° ~ + ' # - _ . , ; ü ä ö ß 9.jpg (missing some characters as before). <b>EDIT 2:</b> I shortened my test string because I suspected filename length "problems" with Opera as there are with IE and it worked there too.</li>
  <li>Safari doesn't work with that syntax. That was excepted. </li>
  </ul>
  
  <h2>EDIT 2:</h2>
  
  <p>Status so far is, that the syntax <b>filename*=UTF-8''filname escape sequence"</b> works with every browser except Safari. And the only character that is getting replaced with Safari is the €. I guess I can live with that. Thank you!</p>
  
  <h2>EDIT 3: Filename length</h2>
  
  <p>I noticed some filename length issues.</p>
  
  <ul>
  <li>Internet Explorer: File names can be 147 characters long. If the string doesn't contain escape sequences then that's the length of the filename. If it does the file name can vary. The resulting file name is shorter that 147 characters. But it differs. I used 2 escape sequences and the file name shortened 5 characters and I used many escape sequences and the file name shortened onyl 2 characters. I couldn't find a rule here. </li>
  <li>The other browsers don't seems to have that problem. They would save the file if the file system can handle it. I tried for instance 250 characters and the browsers said I have to reduce the file name (Chrome) or they did it themselfs shortening it to either 220 (Opera) or 210 (Firefox) characters. Opera cut off the file ending though. Safari tried to save that long file name and ended up not saving it and writing "-1" in the download list as filename. </li>
  </ul>