Solved

ADODB.Stream - File Content is blank on download

Posted on 2006-11-27
4
469 Views
Last Modified: 2008-01-16
I have a simple script taken from another source that prompts a user for a file download rather then attempt to open the file in their browser (if they can) or requiring them to right click 'Save As...' etc

It has been working fine until a user attempts to download a file in IE7. When anyone does the file they download is blank.

Any help would be appreciated.

Thanks.

############# CODE #############
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
      <title>Company Name - File Download Request</title>
</head>

<body>

<%
If Session("USERS_REFLIB") then

      Response.Buffer = True
      Dim strFilePath, strFileSize, strFileName
      Const adTypeBinary = 1

      strFilePath = Server.MapPath("/images/files/")
      strFileName = request("f")

      Response.Clear

      '8*******************************8
      ' Requires MDAC 2.5 to be stable
      ' I recommend MDAC 2.6 or 2.7
      '8*******************************8
      Set objStream = Server.CreateObject("ADODB.Stream")
      objStream.Open
      objStream.Type = adTypeBinary
      objStream.LoadFromFile strFilePath & "/" & strFileName

      strFileType = lcase(Right(strFileName, 4))
      
      ' Feel Free to Add Your Own Content-Types Here
      Select Case strFileType
            Case ".asf"
                  ContentType = "video/x-ms-asf"
            Case ".avi"
                  ContentType = "video/avi"
            Case ".doc"
                  ContentType = "application/msword"
            Case ".zip"
                  ContentType = "application/zip"
            Case ".xls"
                  ContentType = "application/vnd.ms-excel"
            Case ".gif"
                  ContentType = "image/gif"
            Case ".jpg", "jpeg"
                  ContentType = "image/jpeg"
            Case ".wav"
                  ContentType = "audio/wav"
            Case ".mp3"
                  ContentType = "audio/mpeg3"
            Case ".mpg", "mpeg"
                  ContentType = "video/mpeg"
            Case ".rtf"
                  ContentType = "application/rtf"
            Case ".htm", "html"
                  ContentType = "text/html"
            Case ".asp"
                  ContentType = "text/asp"
            Case Else
                  'Handle All Other Files
                  ContentType = "application/octet-stream"
      End Select
      
      
      Response.AddHeader "Content-Disposition", "attachment; filename=" & strFileName
      Response.AddHeader "Content-Length", strFileSize
      ' In a Perfect World, Your Client would also have UTF-8 as the default
      ' In Their Browser
      Response.Charset = "UTF-8"
      Response.ContentType = ContentType
      
      Response.BinaryWrite objStream.Read
      Response.Flush
      
      objStream.Close
      Set objStream = Nothing

Else
      response.write("You are not logged in.")
End If
%>
</body>
</html>
############# CODE #############

0
Comment
Question by:nyk001
[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
  • 2
  • 2
4 Comments
 
LVL 25

Accepted Solution

by:
kevp75 earned 125 total points
ID: 18022960
try commenting out those lines that you are using to set the content type, and just use:
application/octet-stream

I use something very similar to server downloads the code I use is:

Function DownloadFile( strFile, strDownloadFilename )
     Dim strFilename,objStream,objFilesystem,objFilestream
     Dim intFileLength
     strFilename = strFile
     Response.Buffer = True
     Response.Clear
     Set objStream = Server.CreateObject("ADODB.Stream")
     objStream.Open
     objStream.Type = 1
     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
     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
     if Len( Trim(strDownloadFilename) ) > 0 then
          strDownloadFilename = Trim( strDownloadFilename )
     else
          strDownloadFilename = objFilestream.name
     end if
     Response.AddHeader "Content-Disposition", "attachment; filename=" & strDownloadFilename
     Response.AddHeader "Content-Length", intFilelength
     Response.Charset = "UTF-8"
     for i = 0 to objStream.size
          i = i + 128000
          Response.BinaryWrite(objStream.Read(128000))
          Response.Flush
     next
     Set objFilestream = Nothing
End Function



and I simply pass the filename in the querystring, then do:
fID = Request.QueryString("TheFile")
If fID <> "" Then
      strPath = Server.MapPath("/images/files/") & fID 'you may need to add an extra / before this fID
      strFileName = fID
      Call downloadFile(strPath, strFileName)
      response.end()
End If
0
 
LVL 25

Expert Comment

by:kevp75
ID: 18022975
that last couple of lines I just posted, I put that up at the top of the page that displays the links to the files, and then I make the link, link to the page we're on, with the filename as the querystring fID
0
 

Author Comment

by:nyk001
ID: 18026100
I'll give this solution above a go shortly and post my response.

Thanks Kev.

If I can use just 1 type makes me wonder why you have the option to use all the others. ;)
0
 

Author Comment

by:nyk001
ID: 18026183
Worked perfectly!

Thanks again kev!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Date on a table 16 34
Retreiving SOAP FAULT messages using classical ASP 14 29
Hide and Unhide Table 6 25
Converting Stored Procedure to SQL Statement 5 40
I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:   The Exchange of informatio…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

749 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