On my web email server, users can download attachments off their email.
When they receive a file name that is not in English, the text gets broken in IE 8/9, but it comes out totally fine in Chrome/Firefox. Here is the code that currently handles the download:
<%
attachfile = request("file")%>
<%
files = Split(attachfile,"\")
Set fso = CreateObject("Scripting.FileSystemObject")
Response.Clear
'Response.CharSet="UTF-8"
Response.ContentType = "application/unknown"
Response.AddHeader "Pragma", "no-cache"
Response.AddHeader "Expires", "0"
Response.AddHeader "Content-Transfer-Encoding", "binary"
Response.AddHeader "Content-Disposition","attachment; filename = " & files(Ubound(files))
Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Open
objStream.Type = 1
objStream.LoadFromFile attachfile
Response.BinaryWrite objStream.Read
Response.Flush
objStream.Close : Set objStream = nothing
Set fso = Nothing
%>
</BODY>
</HTML>
So then I changed how the filename was being passed in the content-disposition to the following, hoping in vain that it would encode the string in UTF-8 correctly before saving on the client’s computer:
Response.AddHeader "Content-Disposition","attachment; filename = " & Server.URLEncode(files(Ubound(files)))
Now I’ve solved one problem but I am faced with two new problems -_-
The first problem is that filename no longer gets broken when I download the file in IE 8/9 but it adds a weird [1] or [2] at the end of the file extension. So if I had 파일 1.docx, IE would save the document as 파일 1.docx[1]
The second problem is that in chrome, the browser takes the UTF-8 encoded string literally so it gets saved as 파일%20%1.docx. Note that the non-english part of the file is displayed properly but the empty space shows up as Unicode Character Code 20.
How do I go about solving this issue?
Solution
The most browser-compatible approach seems to be to put the desired filename into the url, rather than the
Content-Dispositionresponse header.For example:
When the user clicks on the link above, it will save the correct filename. The following is from IE 8:
So the question becomes – how do you get asp to recognize a url with the above format? The answer is that you use a url re-writer.
If you are using IIS 4 or older, I strongly suggest you upgrade.
I don’t have a lot of experience using URL re-writers, but I was able to get the Microsoft one working easily enough. I used the wizard to add a
user-friendlyrule:The rule itself was easy enough:
As you can see, I am creating a rule that rewrites
to
When the web page (default2.asp) is called, it can grab the filename from the url parameter
f.Unrelated to your question, but you may also want to do some authentication to make sure that only the proper users have access to the file links, and therefore to your files.
Sources
How to encode the filename parameter of Content-Disposition header in HTTP?
How to encode UTF8 filename for HTTP headers? (Python, Django)