Solved

ADODB.Stream from variable rather than from file?

Posted on 2004-10-08
20
3,587 Views
Last Modified: 2012-06-21
I am trying to provide the user with a comma delimited text file from a recordset. So, I have this that gives me the exact content that I want:

objRS.Open strSQL, objConnection
strOutPut = objRS.GetString(2, ,"," , , "Null")

Now, what I want to do is to stream this so that the user will be prompted to download a .txt file with that content.

I know how to do this with a text file, but how do I modify the code below to stream the variable instead?


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<%
Function downloadFile( strFile, strDownloadFilename )
     Dim strFilename,objStream,objFilesystem,objFilestream
     Dim intFileLength
     ' get full path of specified file
     strFilename = server.MapPath(".")  & "\" & strFile
     ' clear the buffer
     Response.Buffer = True
     Response.Clear

     ' create stream
     Set objStream = Server.CreateObject("ADODB.Stream")
     objStream.Open

     ' set as binary
     objStream.Type = 1

     ' check the file exists
     Set objFilesystem = Server.CreateObject("Scripting.FileSystemObject")
     if not objFilesystem.FileExists(strFilename) then
          Response.Write("<h1>Error</h1>: " & strFilename & " does not exist<p>")
          Response.End
     end if


     ' get length of file
     Set objFilestream = objFilesystem.GetFile( strFilename )
     intFilelength = objFilestream.size
 
     objStream.LoadFromFile( strFilename )
     if err then
          Response.Write("<h1>Error: </h1>" & err.Description & "<p>")
          Response.End
     end if
     
     'format strFileName
     if Len( Trim(strDownloadFilename) ) > 0 then
          strDownloadFilename = Trim( strDownloadFilename )
     else
          strDownloadFilename = objFilestream.name
     end if
'     Response.ContentType = "SENTREnet"

     ' send the headers to the users browser
     Response.AddHeader "Content-Disposition", "attachment; filename=" & strDownloadFilename
     Response.AddHeader "Content-Length", intFilelength
     Response.Charset = "UTF-8"

     ' output the file to the browser
'     Response.BinaryWrite objStream.Read
'     Response.Flush
      for i = 0 to objStream.size
            i = i + 128000
            Response.BinaryWrite(objStream.Read(128000))
            Response.Flush
      next

     ' tidy up
     objFilestream.Close
     Set objFilestream = Nothing
End Function
%>
</HEAD>

<BODY>
<%
Call downloadFile("ken.zip", "ken.zip" )
%>
</BODY>
</HTML>
0
Comment
Question by:fritz_the_blank
  • 7
  • 6
  • 6
  • +1
20 Comments
 
LVL 30

Expert Comment

by:VirusMinus
ID: 12261825
looking
0
 
LVL 19

Expert Comment

by:peh803
ID: 12261846
What about writing the data to a text file on the server using FSO, stream it the way you know how, and then when you're done, clean up the file by deleting it using fso?  

I know this is a hack / work around, but what the heck...?

peh803
0
 
LVL 11

Expert Comment

by:thunderchicken
ID: 12261855
I have it in a project I did 2 years ago... looking now
0
 
LVL 46

Author Comment

by:fritz_the_blank
ID: 12261874
>>What about writing the data to a text file on the server using FSO, stream it the way you know how, and then when you're done, clean up the file by deleting it using fso? <<

That I can do and will do if I have no choice. However, it seems much more efficient just to stream the variable rather than writing a text file from the variable, and then stream the text file, and then finally have to delete the file. This is especially true as I will have to create a separate page to take care of the latter--once you start streaming, you can't do anything else with that page....

FtB
0
 
LVL 46

Author Comment

by:fritz_the_blank
ID: 12261877
>>I have it in a project I did 2 years ago... looking now<<

THE BIRD IS BACK!!!!!!!!!!!!!!!!!!!!

FtB
0
 
LVL 19

Expert Comment

by:peh803
ID: 12261900
>>it seems much more efficient just to stream the variable rather than writing a text file from the variable

Yessir, I would tend to agree...let's hope the bird can find his solution from a while back...

peh803
0
 
LVL 11

Accepted Solution

by:
thunderchicken earned 500 total points
ID: 12261904
You just can't use

Response.BinaryWrite Variable ?

Sorry Fritz, it's been 2 years since ASP, just looking through old code...

We used a BinaryWrite Recordset, don't know if this was a blob or something

It was like this:

Response.AddHeader "Content-Disposition", "attachment; filename=" & FileName
Response.Charset = "UTF-8"
Response.ContentType = ContentType
Response.BinaryWrite RS("strItem")
Response.Flush
Response.Clear
0
 
LVL 11

Expert Comment

by:thunderchicken
ID: 12261906
I just hop on every month to get my 3000 points  :)
0
 
LVL 46

Author Comment

by:fritz_the_blank
ID: 12261966
Okay, I am trying this, but all it does is write out the text to the page...

strSQL = "SELECT intVORid, strContactName, strDistName, datReport, strCustName, strModel, strRegNo, strChassisNo, strVORReason, datDTReceived, datRepStarted, datPartsOrdered, strPartNumbers, strPriorityCRef, strDelayCode, intDaysOffRoad, datBackOnRoad FROM tblVOR"
objRS.Open strSQL, objConnection
strOutPut = objRS.GetString(2, ,"," , , "Null")
'response.write(strOutPut)

'Dim objStream,objFilesystem,objFilestream
'Response.Buffer = True
'Response.Clear

' create stream
'Set objStream = Server.CreateObject("ADODB.Stream")
'objStream.Open

' set as binary
'objStream.Type = 1

Response.AddHeader "Content-Disposition", "attachment; filename=VOR_Data.txt"
Response.Charset = "UTF-8"
Response.ContentType = ContentType
Response.BinaryWrite strOutPut
Response.Flush
Response.Clear

objRS.Close
set objRS = Nothing
objConnection.close
set objConnection = Nothing


FtB
0
 
LVL 11

Expert Comment

by:thunderchicken
ID: 12262009
What happens if you change the content type to something else, like excel, or more importantly a csv ?

If it's streaming to the page (showing up the correct info on the browser), then you're in great shape.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 11

Expert Comment

by:thunderchicken
ID: 12262030
I remember we had this problem, just trying to dust off the cob webs off that part of my brain  :)
0
 
LVL 11

Expert Comment

by:thunderchicken
ID: 12262040
Try something like

ContentType = "application/octet-stream"

(Shot in the dark)
0
 
LVL 19

Expert Comment

by:peh803
ID: 12262141
Fritz -- you've maybe seen this, but here's an example from GaryC123 of doing what I suggested earlier...I know you *don't* want to do this and I don't want to shove it down your throat, but here ya go anyways...

from: http://www.experts-exchange.com/Web/Browser_Issues/Q_20790618.html#9701974

<%
Response.Buffer = True

strFileName="ee.txt"  ' Set file name

strFilePath=server.mappath(strFilename) ' Set path of file
set fso=createobject("scripting.filesystemobject")
set f=fso.getfile(strfilepath)
strFileSize = f.size
set f=nothing: set fso=nothing
Const adTypeBinary = 1
Response.Clear
Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Open
objStream.Type = adTypeBinary
objStream.LoadFromFile strFilePath
strFileType = "application/text" ' change to the correct content type for your file
Response.AddHeader "Content-Disposition", "attachment; filename=" & strFileName
Response.AddHeader "Content-Length", strFileSize
Response.Charset = "UTF-8"
Response.ContentType = strFileType
Response.BinaryWrite objStream.Read
Response.Flush
objStream.Close
Set objStream = Nothing
%>
0
 
LVL 19

Expert Comment

by:peh803
ID: 12262228
>>"I know how to do this with a text file, but how do I modify the code below to stream the variable instead?"

After re-reading your original post, I now realize that I should probably just "step aside" and stop suggesting what you already know how to do....

*blush*
0
 
LVL 46

Author Comment

by:fritz_the_blank
ID: 12262264
Okay, we have a winner!!!

<!--#include file="../Connections/dbConnect.asp" -->
<%
Dim objConnection, objRS, strSQL
set objConnection  = Server.CreateObject("ADODB.Connection")
objConnection.Open strConnectionString
Set objRS = Server.CreateObject("ADODB.Recordset")
strSQL = "SELECT intVORid, strContactName, strDistName, datReport, strCustName, strModel, strRegNo, strChassisNo, strVORReason, datDTReceived, datRepStarted, datPartsOrdered, strPartNumbers, strPriorityCRef, strDelayCode, intDaysOffRoad, datBackOnRoad FROM tblVOR"
objRS.Open strSQL, objConnection
strOutPut = objRS.GetString(2, ,"," , , "Null")
objRS.Close
set objRS = Nothing
objConnection.close
set objConnection = Nothing

Response.AddHeader "Content-Disposition", "attachment; filename=Vor.txt"
Response.Charset = "UTF-8"
Response.ContentType = "application/octet-stream"
Response.BinaryWrite strOutPut
Response.Flush
Response.Clear
%>
0
 
LVL 46

Author Comment

by:fritz_the_blank
ID: 12262277
@peh803--

Thank you very much for the code. As you can see, though, the code I posted above already does that.

I have built my suggestions on Thunderchicken's posts. If everyone is okay with it, I would like to award him the points.

FtB
0
 
LVL 19

Expert Comment

by:peh803
ID: 12262278
Great, nice work thunderchicken!  I guess you only need 1000 more points to get up to your 3000 points for the month...

:)

peh803
0
 
LVL 11

Expert Comment

by:thunderchicken
ID: 12262293
Yes, fine with me  :)

I can go over my 3000 and go back into hiding
0
 
LVL 46

Author Comment

by:fritz_the_blank
ID: 12262331
Thanks again to all!

FtB
0
 
LVL 19

Expert Comment

by:peh803
ID: 12262332
Yeah, of course!  Totally fine with me.  Me telling you stuff you already know doesn't add a whole lot of value...

I just tested your code based on thunderchicken's posts, and that is *cool* stuff.

Thanks,
peh803
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now