Created
March 13, 2014 11:24
-
-
Save bennadel/9526663 to your computer and use it in GitHub Desktop.
Embedding Foreign Characters In Your Content-Disposition Filename Header
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!--- | |
Query for the files in the directory. | |
-- | |
NOTE: I am doing this so I don't have to embed high-ascii characters | |
in the code - I don't think my blog has the proper support for UTF-8 | |
encoding? Not sure. | |
---> | |
<cfset files = directoryList( expandPath( "./" ), false, "name", "Data*" ) /> | |
<!--- Isolate the file with foreign characters. ---> | |
<cfset fileName = files[ 1 ] /> | |
<cfset filePath = expandPath( fileName ) /> | |
<!--- | |
By default, the filename portion of the Content-Disposition header | |
only allows for US-ASCII values. In order to account for foreign / | |
exnteded ASCII values, we have to jump through some funky notation. | |
In this case, we are attempting to provide fallbacks. The first | |
instance of "filename" is for browsers that do not support the RFC | |
5987 encoding (they ignore the filename*= after the filename). | |
Then, for browsers that DO support the encoding, they will pick | |
up the UTF-8 encoding. | |
Notice that the UTF-encoded value doesn't need to be quoted since | |
the embeded spaces are url-encoded. | |
---> | |
<cfheader | |
name="content-disposition" | |
value="attachment; filename=""#fileName#""; filename*=UTF-8''#urlEncodedFormat( fileName )#" | |
/> | |
<cfcontent | |
type="text/plain; charset=utf-8" | |
file="#filePath#" | |
/> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment