Solved

Downloading File with Unicode File Name Causes Problem

Posted on 2006-11-05
5
573 Views
Last Modified: 2008-03-17
In order to track file download rates, for all the files available for download on my web page I've placed a link to an ASP page which increments the download counter. Within the same page, it reads the binary data from file and uses following code to display the prompt window so user can either choose to save or open the file.

Response.CharSet = "UTF-8"
Response.AddHeader "Content-disposition", "attachment;filename=" & stFileName
Response.BinaryWrite(bytFile)
Response.Flush
Response.Clear

The code works fine but whenever the file name contains unicode characters, file name displayed and saved on the prompt window is not the original name, rather it displayed some alphanumeric characters.

If I use Response.Redirect to the given file name, the prompt windows gives the correct result, but I cannot prevent user browser from opening the know MIME types within the browser itself.

I would like to find out how I can solve the unicode filename problem.

Thanks in advance!
0
Comment
Question by:guest321
5 Comments
 
LVL 25

Expert Comment

by:kevp75
ID: 17876724
interestign conundrum....

what happnes if you comment out this line:
Response.CharSet = "UTF-8"

also, I've found it best to stream the file to the browser using the ADO.Stream method.  This may be a better route and may cure the unciode filename issue

basically you pass the path to the file, and then specify the filename.

Here is the function, and how to use it:

<%
Function DownloadFile( strFile, strDownloadFilename )
     Dim strFilename,objStream,objFilesystem,objFilestream
     Dim intFileLength
     strFilename = strFile
     Response.Buffer = True
     Response.Clear
     Set objStream = Server.CreateObject("ADODB.Stream")
     objStream.Open
     objStream.Type = 1
     Set objFilesystem = Server.CreateObject("Scripting.FileSystemObject")
     if not objFilesystem.FileExists(strFilename) then
          Response.Write("<h1>Error</h1>: " & strFilename & " does not exist<p>")
          Response.End
     end if
     Set objFilestream = objFilesystem.GetFile( strFilename )
     intFilelength = objFilestream.size
     objStream.LoadFromFile( strFilename )
     if err then
          Response.Write("<h1>Error: </h1>" & err.Description & "<p>")
          Response.End
     end if
     if Len( Trim(strDownloadFilename) ) > 0 then
          strDownloadFilename = Trim( strDownloadFilename )
     else
          strDownloadFilename = objFilestream.name
     end if
     Response.AddHeader "Content-Disposition", "attachment; filename=" & strDownloadFilename
     Response.AddHeader "Content-Length", intFilelength
     Response.Charset = "UTF-8"
     for i = 0 to objStream.size
          i = i + 128000
          Response.BinaryWrite(objStream.Read(128000))
          Response.Flush
     next
     Set objFilestream = Nothing
End Function
%>

to use it:
<%
strPath = "c:\the\path\to\thefile.ext"
theFileToDownload = "thefilenameyouwanttocallit.ext"
Call downloadFile(strPath, theFileToDownload)
%>

HTAH
0
 
LVL 58

Expert Comment

by:Gary
ID: 17876773
I always  use..

<%
strfileName = "someobject.pdf"

Response.ContentType = "application/octet-stream"
Response.AddHeader "content-disposition","attachment;filename=" & strfileName

Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Open
objStream.Type = 1 '1=binary 2=text
objStream.LoadFromfile Server.MapPath(strfileName)

Response.BinaryWrite objStream.Read

objStream.Close
Set objStream = Nothing
%>
0
 
LVL 6

Accepted Solution

by:
gete earned 125 total points
ID: 17878717
RFC 2183, Section 2:
http://www.ietf.org/rfc/rfc2183.txt

   NOTE ON PARAMETER VALUE LENGHTS: A short (length <= 78 characters)
   parameter value containing only non-`tspecials' characters SHOULD be
   represented as a single `token'.  A short parameter value containing
   only ASCII characters, but including `tspecials' characters, SHOULD
   be represented as `quoted-string'.  Parameter values longer than 78
   characters, or which contain non-ASCII characters, MUST be encoded as
   specified in [RFC 2184].

You can see the encoding spec in RFC 2231 (which obsoletes RFC 2184).
http://www.ietf.org/rfc/rfc2231.txt

For more example about implementation, you can visit http://www.codeproject.com/aspnet/NonUSASCII.asp
(unfortunately, it's in ASP.NET/C#).
0
 

Author Comment

by:guest321
ID: 17887307
Thank you everyone for the help! The problem has been solved by enclosing the file name with Server.URLEncode() method (also mentioned as Encoded-word mechanism in gete's given url).
0
 
LVL 25

Expert Comment

by:kevp75
ID: 17889158
glad we could help :|
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now