• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 485
  • Last Modified:

ADODB.Stream - File Content is blank on download

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
nyk001
Asked:
nyk001
  • 2
  • 2
1 Solution
 
kevp75Commented:
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
 
kevp75Commented:
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
 
nyk001Author Commented:
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
 
nyk001Author Commented:
Worked perfectly!

Thanks again kev!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now