I am trying to return a binary file through ASP (it will be created from a database query) and am having trouble with the conversion to binary data (ascii string instead of unicode). If in fact there's a better way then this I'd love to hear it (I know this method can have a problem with large files but I won't be reaching anywhere near that size).
I am getting an error:
[ul]ADODB.Stream (0x800A0BB9)
Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another.[/ul]
with the following code (pared down to bare minimum). Erroring line is in blue. I've done web searches on this error but what turned up was unhelpful.
I could do this as text, but I felt it was better to do it as binary, since I cannot guarantee that the data I'll get from the database will never be above 127. For that matter, I'd be interested to know how to do unicode, but I suppose that's another question entirely.
I've searched many a web site for help on this and I haven't been able to find where I'm going wrong. The StringToMultiByte function is lifted intact from some page or other online. The response clearing, writing, content type and header stuff works fine.
I am getting an error:
[ul]ADODB.Stream (0x800A0BB9)
Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another.[/ul]
with the following code (pared down to bare minimum). Erroring line is in blue. I've done web searches on this error but what turned up was unhelpful.
Code:
<HTML><HEAD></HEAD>
<BODY></BODY>
</HTML>
<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<%
Function StringToMultiByte(S)
Dim i, MultiByte
For i=1 To Len(S)
MultiByte = MultiByte & ChrB(Asc(Mid(S,i,1)))
Next
StringToMultiByte = MultiByte
End Function
Sub WriteStream(TheStream, TheText)
[blue] TheStream.Write StringToMultiByte(TheText)[/blue]
End Sub
Dim oStream
Set oStream = Server.CreateObject("ADODB.Stream")
oStream.Open
oStream.Type = 1 ' 1=binary 2=text
WriteStream oStream, "<html xmlns:x=""urn:schemas-microsoft-com:office:excel""><head><meta http-equiv=Content-Type content=""text/html; charset=us-ascii"">"
WriteStream oStream, "{more excel html document building code which works fine}"
WriteStream oStream, "</html>"
oStream.Position = 0
Response.Clear
Response.AddHeader "Content-Disposition", "attachment; filename=MyFileName.xls"
Response.AddHeader "Content-Length", oStream.Size
Response.ContentType = "application/octet-stream"
Response.CharSet = "UTF-8"
Response.BinaryWrite oStream.Read
Response.Flush
oStream.Close
Set oStream = Nothing
%>
I've searched many a web site for help on this and I haven't been able to find where I'm going wrong. The StringToMultiByte function is lifted intact from some page or other online. The response clearing, writing, content type and header stuff works fine.