Results 1 to 6 of 6
  1. #1
    Join Date
    Oct 2003
    Location
    UK London
    Posts
    4

    Question Unanswered: 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 Attached Files

  2. #2
    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.

  3. #3
    Join Date
    Oct 2003
    Location
    UK London
    Posts
    4
    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....

  4. #4
    Join Date
    Jul 2003
    Location
    SoCal
    Posts
    721
    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.

  5. #5
    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....

  6. #6
    Join Date
    Oct 2003
    Location
    UK London
    Posts
    4
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •