Solved

ADODB.Stream - File Content is blank on download

Posted on 2006-11-27
4
463 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
  • 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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 information …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now