guest321
asked on
Downloading File with Unicode File Name Causes Problem
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(bytFi le)
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!
Response.CharSet = "UTF-8"
Response.AddHeader "Content-disposition", "attachment;filename=" & stFileName
Response.BinaryWrite(bytFi
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!
I always use..
<%
strfileName = "someobject.pdf"
Response.ContentType = "application/octet-stream"
Response.AddHeader "content-disposition","att achment;fi lename=" & 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
%>
<%
strfileName = "someobject.pdf"
Response.ContentType = "application/octet-stream"
Response.AddHeader "content-disposition","att
Set objStream = Server.CreateObject("ADODB
objStream.Open
objStream.Type = 1 '1=binary 2=text
objStream.LoadFromfile Server.MapPath(strfileName
Response.BinaryWrite objStream.Read
objStream.Close
Set objStream = Nothing
%>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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).
glad we could help :|
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,objF
Dim intFileLength
strFilename = strFile
Response.Buffer = True
Response.Clear
Set objStream = Server.CreateObject("ADODB
objStream.Open
objStream.Type = 1
Set objFilesystem = Server.CreateObject("Scrip
if not objFilesystem.FileExists(s
Response.Write("<h1>Error<
Response.End
end if
Set objFilestream = objFilesystem.GetFile( strFilename )
intFilelength = objFilestream.size
objStream.LoadFromFile( strFilename )
if err then
Response.Write("<h1>Error:
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(objSt
Response.Flush
next
Set objFilestream = Nothing
End Function
%>
to use it:
<%
strPath = "c:\the\path\to\thefile.ex
theFileToDownload = "thefilenameyouwanttocalli
Call downloadFile(strPath, theFileToDownload)
%>
HTAH