2

I need to do a script in classic ASP to generate a CSV file for the user to download. It needs to be encoded in "classic Windows Unicode", ie., UTF-16.

I tried this:

  Response.Clear
  Response.ContentType = "text/csv"
  Response.Charset = "utf-16"
  Response.Codepage = 1200
  Response.AddHeader "Content-Disposition", "attachment; filename=export.csv"

but I get the error

ERROR -2147467259: 006~ASP 0204~Invalid CodePage Value~An invalid CodePage value was specified.

which makes sense, because according to the documentation, code page 1200 is only available to managed (ASP.NET) applications.

But then, how can I set the Response's charset to UTF-16?

angus
  • 2,305
  • 1
  • 15
  • 22
  • 1
    You seem clued up so this might not be relevant but I don't think you can get ASP to support UTF-16 (see update on [this answer](http://stackoverflow.com/a/25646123/692942)). Sorry for being the bearer of bad news. – user692942 Sep 16 '16 at 15:23
  • @Lankymart Thanks, that's too bad. Do you think it would be possible to achieve it using a stream and Response.BinaryWrite, or something like that? – angus Sep 16 '16 at 16:27
  • To be honest @angus it would be easier to either write a COM component to do it in something like .Net then call it from ASP or try to get the requirement to accept UTF-8 instead. Good luck though, sorry that isn't much help. – user692942 Sep 16 '16 at 19:20

1 Answers1

0

Well, I “solved” it using an ADODB.Stream. The disadvantage of this is that I need to accumulate the full output in memory before I send it to the user. Basically,

dim s
set s = Server.CreateObject("ADODB.Stream")
s.Type = 2  ' text '
s.Open
do while <getting data>
  s.WriteText <data>
loop
s.Flush
s.Position = 1
s.Type = 1   ' binary '

Response.Clear
Response.ContentType = "application/csv"
Response.AddHeader "Content-Disposition", "attachment; filename=export.csv"

Response.BinaryWrite s.Read

This makes use the fact that ADODB.Stream uses Unicode by default to store text.

angus
  • 2,305
  • 1
  • 15
  • 22