Solved

ADODB.Stream from variable rather than from file?

Posted on 2004-10-08
20
3,597 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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
 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Cannot open file from ASP if user is "logged in" 13 34
Call a function within the ASP code 4 26
Hide Table in merge 3 42
Load data upon clicking a button 8 48
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…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

726 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