HTTP headers and non-asci characters (Content-Disposition, filename, attachment) Article

Member of  ScriptUtils.ByteArray | Changes | Purchase | Download

HTTP headers and non-asci characters (Content-Disposition, filename, attachment)

Some ways to download file names with international (non-asci) characters.

    The main problem for file downloads is to set right file name with good international characters. The downloaded file name is specified by a filename parametr of Content-Disposition http header.

    There is no problem if you set only ASCII characters for the parametr. you can use

Response.AddHeader "content-disposition", "attachment; filename=anyascicharactersyouwant.txt"

    But the problem is when you try to use any non-ascii characters in the downoaded file name. Sometimes you will need some russian file name: ???_?????.txt, sometimes chinese 文件名 ???, japanese ファイル名, korean 파일 이름 or some Cental european filename (Czech) příliš_žlutý_kůň_úpěl_ďábelské_ódy.txt.

    I was not able to find universal settings to do this task, but it looks like Mozilla based browsers accepts utf-8 encoded headers and headers Encoded Word Extensions from RFC 2231. Internet explorer accepts utf-8 filenames only when 1. the data are URL encoded and 2. the extension of the file contains only ASCII characters.

    Content-Disposition - test Non ascii characters

Sub SetContentDisposition(byval FileName)
  'set response. charset 
  Response.CharSet = "utf-8"
  'set ContentType - download
  Response.ContentType = "application/download"

  'URLEncode the filename for IE
  If Instr(1, Request.ServerVariables("HTTP_USER_AGENT"), "MSIE", 1)>0 Then
    FileName = URLEncode(FileName,"utf-8")
  End If
  
  'Add a Content-Disposition header with the filename
  Response.AddHeader "Content-Disposition", _
   "attachment; filename=""" & FileName & """"
End Sub

'URL Encode with character set 
Function URLEncode(ByVal Data, CharSet)
    Dim ByteArray
    Set ByteArray = CreateObject("ScriptUtils.ByteArray")
    ByteArray.CharSet = CharSet
    
    ByteArray.String = Data

  If Len(Data) > 0 Then
    Dim I, C, Out
    
    For I = 1 To ByteArray.Length
      C = ByteArray(I)'Asc(Mid(Data, I, 1))
      If C = 32 Then
        Out = Out + "+"
      ElseIf C < 46 Or c>126 Then
        Out = Out + "%" + Hex(C)
      Else
        Out = Out + Chr(c)
      End If
    Next
    URLEncode = Out
  End If
End Function		

Other links for HTTP headers and non-asci characters (Content-Disposition, filename, attachment) article

ScriptUtils.ByteArray

Works with safearray binary data - save/restore binary data from/to a disk, convert to a string/hexstring, codepage/charset conversions, Base64 conversion, etc.
     ByteArray is a COM class specially designed to work with Microsoft Windows Scripting engines - VB Script and JScript in Active Server Pages or WSH and in CHM or HTA applications. It also works with VB Net, Visual basic (VBA - VB 5, VB 6, Word, Excel, Access, ), C#, J#, C++, ASP, ASP.Net, Delphi and with T-SQL OLE functions - see Use ByteArray object article. You can also use the object in other programming environments with COM support, such is PowerBuilder.
     Source code for ByteArray is available within distribution license, please see License page for ASP file upload and ScriptUtilities.

ScriptUtils

Huge ASP upload is easy to use, hi-performance ASP file upload component with progress bar indicator. This component lets you upload multiple files with size up to 4GB to a disk or a database along with another form fields. Huge ASP file upload is a most featured upload component on a market with competitive price and a great performance . The software has also a free version of asp upload with progress, called Pure asp upload , written in plain VBS, without components (so you do not need to install anything on server). This installation package contains also ScriptUtilities library. Script Utilities lets you create hi-performance log files , works with binary data , you can download multiple files with zip/arj compression, work with INI files and much more with the ASP utility.

© 1996 - 2014 Antonin Foller, Motobit Software | About, Contacts | (Find us on Google+)


Other Motobit links:   IISTracer, real-time IIS monitor   ASP file upload - upload files to ASP. 
ActiveX/VBSScript registry editor  ActiveX NT User account manager  Export MDB/DBF from ASP Active LogFile  Email export  ActiveX/ASP Scripting Dictionary object

Motobit.com