[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

ADODB.Stream - File Content is blank on download

Posted on 2006-11-27
4
Medium Priority
?
483 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 500 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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 would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

649 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