If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ASP > ASP SQL 2000 Binray File Image Download

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-09-03, 16:02
Darcey Darcey is offline
Registered User
 
Join Date: Oct 2003
Location: UK London
Posts: 4
Question ASP SQL 2000 Binray File Image Download

Hi, am trying to get a file download from a SQL 2000 database Win 2K. I am not too clear on the creation of the DB (as my colleague built it) but I am attempting to access a file download.

I have got file uploads working but I am having trouble downloading the binary stream.

I keep getting the output "AdVarBinary" written back to me.

The code for my download is as follows:

FILE NAME: VIEW.ASP
****START OF VIEW.ASP****

<% Response.Buffer = True %>
<!--#include virtual="/uk/smb_calendar/includes/functions/database.asp" -->
<!--#include virtual="/uk/smb_calendar/includes/functions/exec_stored.asp" -->
<%
'EXEC STROED PROCEDURE
dim params(0)
params(0) = request("UploadID")

dim blnSucceeded
blnSucceeded = getRS("GetAnUpload",params,rs)

'PROCESS SOME VARIABLES
strfilename = rs("filename")
temp = split(filename,".")
fileExtensions = CStr(temp(1))

'STORED PROC RETURNS THE FOLLOWING
'rs("FileImage") - FILE IMAGE
'rs("filename") - the file name

'SUPPOSEDLY START THE FILE DOWNLOAD
'SHOULDNT HARD CODE CONTENT TYPE
Session("ContentType") = "text"
Session("FileImage") = rs("FileImage")

response.redirect("test.asp")
%>
****END OF VIEW.ASP****


FILE NAME: TEST.ASP
****START OF TEST.ASP****
<%
response.Expires = 0
response.Buffer = True
response.Clear

Response.AddHeader "content-disposition","attachment; filename='test.txt'"
response.contentType = Session("ContentType")
response.BinaryWrite Session("FileImage")

Session("ContentType") = ""
Session("FileImage") = ""

response.End
%>
****END OF TEST.ASP****

Can anyone help me on this? I am stuck on this one.
Any ideas please post or email Darcey@tidal.co.uk

Thanks
Attached Files
File Type: zip download.zip (895 Bytes, 52 views)
Reply With Quote
  #2 (permalink)  
Old 10-09-03, 16:17
Seppuku Seppuku is offline
Useless...
 
Join Date: Jul 2003
Location: SoCal
Posts: 721
Is the file stored in the database, or just a pointer to a file stored in the filesystem?
__________________
That which does not kill me postpones the inevitable.
Reply With Quote
  #3 (permalink)  
Old 10-10-03, 04:42
Darcey Darcey is offline
Registered User
 
Join Date: Oct 2003
Location: UK London
Posts: 4
Quote:
Originally posted by Seppuku
Is the file stored in the database, or just a pointer to a file stored in the filesystem?
It's in the database....
Reply With Quote
  #4 (permalink)  
Old 10-10-03, 11:36
Seppuku Seppuku is offline
Useless...
 
Join Date: Jul 2003
Location: SoCal
Posts: 721
Quote:
Originally posted by Darcey
It's in the database....
Ok.. first of all, don't use session variables to store information that is not necessary. My personal rule of thumb is to leave the session/cookie collection strictly for indentifying information. Then I use code to reference that identifying information to retrieve additional data as needed.

If you're attempting to download an image INTO the browser, you can stream the image directly to the user. If you want to download the image as a file to be saved by the user, you need to send it to them as an attachement to the page.

If you're downloading into the browser, you need to create an ASP file that does simply that... for example "imageViewer.asp". That ASP would take a querystring parameter that identifies the file you want to retrieve. That entire URL (filename + paramters) is the "SRC" for your <IMG> tag: <IMG SRC="imageViewer.asp?ID=1">

The code for your "imageViewer.asp" takes this querystring parameter, queries the DB for the record, pulls the binary image, sets the header properties that will define this as an image, then send it to the user. Here's a minimal example of what that would look like:

imageViewer.asp
Code:
<%
  Dim iImgID
  iImgID = Request.QueryString("ImgID")

  Response.Expires = 0
  Response.Buffer = TRUE
  Response.Clear

  ''' Do you DB Query to pull your image record based on the iImgID '''

  If NOT rs.BOF AND NOT rs.EOF Then
    If rs("FileImage").ActualSize > 0 Then
      Response.ContentType = "image/gif"
      Response.AddHeader "Content-Length", Len(rs("FileImage"))
      Response.BinaryWrite rs("FileImage")
    End If
  End If

  Response.End
So now whenever you have an <IMG> tag that points to that URL, it'll download the image from the DB, and load it into the browser as if it were coming off the file system. You can get more sophisticated with this as well. I've found plenty of stuff online that will take a binary file from a DB and tell you what the content-type is, the height/width, etc.

Now, if you want to download that image as an attachment, that's different. That means the user will be prompted for a location to save the file instead of displaying it directly in the browser. In this case, you'd have an <A HREF> link that pointed to an ASP with a QueryString much like the example above. Keep in mind though that changes to the header can only be done before any content has been sent to the browser. Response.Redirect is a header, and therefore, you cannot redirect the user after the download has been completed. It's best to use an <IFRAME> and a TARGET="" in your <A HREF> to do the download of files. Here's an example of downloading an attachment:

dlFile.asp
Code:
Dim iFileID
iFileID = Request.QueryString("FileID")

''' Do your DB query for the file based upon the FileID '''

If NOT rs.BOF AND NOT rs.EOF Then
  Response.Clear
  Response.Buffer = True
  Response.ContentType = "image/jpeg"
  Response.AddHeader "Content-Disposition", "attachment; filename=" & rs("FileName")

  Response.AddHeader "Content-Length", Len(rs("FileImage"))

  Response.BinaryWrite rs("FileImage")
  Response.Flush

End If
Again, you can get much more intricate, but that should force the file to be downloaded to the computer.

Keep in mind the difference between the two... one loads the image into the browser... the other forces the image to be downloaded to the computer.

I hope that helps!
__________________
That which does not kill me postpones the inevitable.
Reply With Quote
  #5 (permalink)  
Old 10-10-03, 19:14
Darcey Darcey is offline
Registered User
 
Join Date: Oct 2003
Location: UK London
Posts: 4
The file to be downloaded from the SQL DB (IMAGE) can be anything not just for web page output.. It is a file archive area for a campaign on a calendar....
Reply With Quote
  #6 (permalink)  
Old 10-10-03, 19:16
Darcey Darcey is offline
Registered User
 
Join Date: Oct 2003
Location: UK London
Posts: 4
Quote:
Originally posted by Darcey
The file to be downloaded from the SQL DB (IMAGE) can be anything not just for web page output.. It is a file archive area for a campaign on a calendar....
*************************
Problem solved.. Was in my handling of the exec stored procedure - type.....
thank you for the help though..
I must have coded several different ways of doing this.. all worked.. lol
typical eh..

Much appreciate your help though.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On