locked
Download fails, whats wrong with this script? RRS feed

  • Question

  • User-270348295 posted

    I have a particular script for downloading files, it has worked for ages on IIS 6.0, as soon i transferred the script to a IIS 7.0 box the script no longer worked...
    Whenever i put in a reponse write befre the download starts the download works again, wtf?

    In the sample below shows my code, if i enable the bold line the script works fine, if i don't it fails... anyone can shed a light on this?

    <%@LANGUAGE="VBSCRIPT"%>
    <% OPTION EXPLICIT %>
    <!-- #include file="lib/datafunc.asp" -->
    <!-- #include file="lib/functions.asp" -->
    <!-- #include file="lib/logevent.asp" -->
    <%
    Server.ScriptTimeout = 5400 '90 min
    if session("user")<>"" Then
    Call OpenDB()
     Dim file, userpath
     if request("zip")="true" then
      file = Request.QueryString("name")
      userpath = server.mappath("temp") & "\"
     Else
      Dim sql, rsf
      file = Request.QueryString("file")
      Set rsf = server.createObject("ADODB.RecordSet")
      'rs.CursorLocation = 3 'adUseClient
      'rs.CursorType = 0 'adOpenForwardOnly
      sql = "SELECT * FROM tblUser WHERE user_id=" & session("user")
      rsf.open sql,conn
      If Not rsf.eof Then
       userpath = rsf("user_url")
      End If
      rsf.Close
      Set rsf = nothing
     end If

     Response.write "if i enable this line it works, what the hell?"
     
     Dim filePath, objFilesystem, objFilestream, intFilelength, objStream
     
     If file<>"" Then
      'replace spaces with underscores
      file = Replace(file," ","_")

      Set objFilesystem = Server.CreateObject("Scripting.FileSystemObject")
      filePath = userpath & Request.QueryString("path") & Request.QueryString("file")

      On Error Resume Next
      If Not Response.isClientConnected Then
       Response.End
      End If

      Set objFilestream = objFilesystem.GetFile(filePath)
      intFilelength = objFilestream.size

      Set objStream = Server.CreateObject("ADODB.Stream")
      objStream.Open
      objStream.Type = 1 'adTypeBinary
      objStream.LoadFromFile objFilestream 'filePath

      Response.Clear
      Response.Buffer = False
      Response.ContentType = GetMimeType(fileExt(Request.QueryString("file")))
      Response.AddHeader "Content-Disposition", "attachment; filename=" & file 'objFilestream.name
      Response.AddHeader "Content-Length", intFilelength
      'stream file in blocks
      do until objStream.eos Or Not Response.isClientConnected
       response.binaryWrite objStream.read (4000000)
      Loop
      Response.Flush

      'Log file downloads
      Call LogEvent("Download",userpath & Request.QueryString("path") & Request.QueryString("file"),session("userName"),Request.ServerVariables("REMOTE_ADDR"))
      
      Response.End
      objStream.Close
      Set objStream = Nothing
      
      if request("zip")="true" then
       if fso.fileexists(userpath & Request.QueryString("path") & Request.QueryString("file")) then
        fso.deletefile(userpath & Request.QueryString("path") & Request.QueryString("file"))
       end if
      end If
      Set objFilesystem = Nothing
     Else
      response.write "Undefined error!"
     End If
    else
     response.redirect "index.asp"
    end If
    Call CloseDB()%>

    Thursday, August 14, 2008 5:58 PM

Answers

  • User1073881637 posted

    What you are doing is more or less 'flushing' the buffer to the client.  If this works on IIS 6, can you verify if buffering is turned off or ?  I see you are doing it later in the code and flushing.

    • Marked as answer by Anonymous Tuesday, September 28, 2021 12:00 AM
    Sunday, August 17, 2008 10:28 PM
  • User-270348295 posted

    Weird... Buffer was on on both IIS 6 and IIS 7... allthough the script worked fine on IIS 6 it dind't on IIS7

    I eventually fixed it by setting the Response.Buffer to True and moving the flush up a line...now it works again, thanks for your help on this

      Response.Buffer True

      'stream file in blocks
      do until objStream.eos Or Not Response.isClientConnected
       response.binaryWrite objStream.read (4000000)
       response.flush
      Loop

     

    • Marked as answer by Anonymous Tuesday, September 28, 2021 12:00 AM
    Tuesday, August 19, 2008 3:54 PM

All replies

  • User1073881637 posted

    Interesting.  couple things come to mind to check.

    1) Is your application pool running in integrated mode?  Have you tried switching to Classic Mode and see if the behavior is the same.

    2) Is dynamic compression enabled?

    Friday, August 15, 2008 1:12 AM
  • User-270348295 posted

    1) Yes it was running in integrated mode, after swithing to Classic mode results are identical.
    2) No, default install (read somewhere dynamic compression is disabled by default, right?)

    Friday, August 15, 2008 3:17 AM
  • User1073881637 posted

    What you are doing is more or less 'flushing' the buffer to the client.  If this works on IIS 6, can you verify if buffering is turned off or ?  I see you are doing it later in the code and flushing.

    • Marked as answer by Anonymous Tuesday, September 28, 2021 12:00 AM
    Sunday, August 17, 2008 10:28 PM
  • User-270348295 posted

    Weird... Buffer was on on both IIS 6 and IIS 7... allthough the script worked fine on IIS 6 it dind't on IIS7

    I eventually fixed it by setting the Response.Buffer to True and moving the flush up a line...now it works again, thanks for your help on this

      Response.Buffer True

      'stream file in blocks
      do until objStream.eos Or Not Response.isClientConnected
       response.binaryWrite objStream.read (4000000)
       response.flush
      Loop

     

    • Marked as answer by Anonymous Tuesday, September 28, 2021 12:00 AM
    Tuesday, August 19, 2008 3:54 PM
  • User1073881637 posted

    The default behavior in IIS is to buffer=true.  You might not need that line even and flush when you want to send to the client.  Just a thought even though I don't think it hurts anything.  Glad you got it resolved.

    Tuesday, August 19, 2008 10:01 PM