Solved

ADODB.Stream - File Content is blank on download

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

776 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