Skip to main content
Inspiring
July 16, 2007
Question

steam text to file download

  • July 16, 2007
  • 2 replies
  • 405 views
I'm re-posting this as it's a new week and I'm still a little stuck.

Basically, I need to stream content to a file (CSV) and deliver straight to
the user (no server storage). I've tried the following both on my local XP
machine and the remote server (windows server 2003). In both cases the
script creates a file for download but the file is empty. Any illumination
would be much appreciated.

<%
Response.ContentType = "text/csv"
Dim objStream
Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Type = 2
objStream.Open
objStream.WriteText "this is my content"
Response.addHeader "content-disposition","attachment;filename=myfile.csv"
Response.BinaryWrite objStream.ReadText
objStream.Close
Set objStream = Nothing
%>

Many thanks

Justin


This topic has been closed for replies.

2 replies

Inspiring
July 16, 2007
Fantastic, still unsure why mine didn't work - but hey, if it works, it
works - who am I to question...

Many thanks
:-)


Participating Frequently
July 16, 2007
As this is not by way of a stream, per say, but this is working on my Server 2003 in IE, Firefox (PC and Mac) and Safari. The If for user agent is due to some limitations for the "Content-Disposition" statement and IE user-agents. Never did actually find out why, but it works:

' I build an array while looping through my record set
' then I build a body to actually export to the user
for i=0 to Ubound(arrReturnedRecs,2)
for j=0 to Ubound(arrReturnedRecs,1)
If arrReturnedRecs(j,i) <> "" then
strBody = strBody & Chr(34) & uCase(arrReturnedRecs(j, i)) & Chr(34) & ","
Else
strBody = strBody & Chr(34) & "N/A" & Chr(34) & ","
End if
next
strBody = strBody & vbcrlf
next

' the strHeader is my column names, previously built
strOutPut = strHeader & strBody

Response.Clear
Response.ContentType = "text/csv.ms-excel"

userAgent = request.serverVariables("HTTP_USER_AGENT")
If InStr(userAgent, "MSIE") then
Response.AddHeader "Content-Disposition", "filename=" & Chr(34) & "YourFileName.csv;" & Chr(34)
Else
Response.AddHeader "Content-Disposition", "filename=YourFileName.csv;"
End if

Response.Write(strOutPut)
Response.End()

' delete obj's, clear var's, etc.