Files corrupted using BinaryWrite

I have an application that includes functions to stream downloads to users, several functions are available but the one used to stream larger file sizes is:

' ******************************************************

' Binarywrite but periodically flush cache
Function downloadFile( strFile, strDownloadFilename )
     Dim strFilename,objStream,objFilesystem,objFilestream
     Dim intFileLength
     ' get full path of specified file
     strFilename = server.MapPath(strFile)
     ' clear the buffer
     Response.Buffer = True

     ' create stream
     Set objStream = Server.CreateObject("ADODB.Stream")

     ' set as binary
     objStream.Type = 1

     ' check the file exists
     Set objFilesystem = Server.CreateObject("Scripting.FileSystemObject")
     if not objFilesystem.FileExists(strFilename) then
          Response.Write("<h1>Error</h1>: " & strFilename & " does not exist<p>")
     end if

     ' get length of file
     Set objFilestream = objFilesystem.GetFile( strFilename )
     intFilelength = objFilestream.size
     objStream.LoadFromFile( strFilename )
     if err then
          Response.Write("<h1>Error: </h1>" & err.Description & "<p>")
     end if
     'format strFileName
     if Len( Trim(strDownloadFilename) ) > 0 then
          strDownloadFilename = Trim( strDownloadFilename )
          strDownloadFilename =
     end if

     ' send the headers to the users browser
     Response.AddHeader "Content-Disposition", "attachment; filename=" & strDownloadFilename
     Response.AddHeader "Content-Length", intFilelength
     Response.Charset = "UTF-8"

     ' output the file to the browser
'     Response.BinaryWrite objStream.Read
'     Response.Flush
     for i = 0 to objStream.size
          i = i + 128000

     ' tidy up
     Set objFilestream = Nothing
End Function

' ******************************************************

While this works ok most of the time, i am getting reports from certain customers that files are getting corrupted during the download process. I have not managed to replicate this problem myself but the frequency and reliability of reports suggests that this is indeed happening, the latest user to report it is downloading .exe's of around 7mb, if i try the download from my machine here it runs fine, all 4 machines at their office however end up with a corrupted file.

Can anyone think what is causing this and suggest a solution that will work with all browsers and still keep the URL masked?

PS - The other function i use does not cause this issue but also does not work with files larger than a few meg, this function is:

' Use binarywrite for smaller files
Set adoStream = Server.CreateObject("ADODB.Stream")      
      adoStream.Type = 1
      adoStream.LoadFromFile Server.Mappath(SLDownloadFileName)
      If adoStream.Type = 1 Then
            stream = adoStream.Read()
            stream = adoStream.ReadText()
      End If
      ' tidy up
      Set adoStream = Nothing
      Response.Buffer = True
      Response.AddHeader "Content-Disposition", "attachment; filename=" & RSFileDownload.Fields.Item("D2DownloadPath").Value
      Response.ContentType = FileContentType
      Response.BinaryWrite stream

(Just so no-one suggests using a function like that one).

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

If the problem appears to be at a particular office, might it be their firewall causing the issue?

If they retry the same file can they successfully download it?

You may want to get them to try a download manager and see if this helps.

PD69Author Commented:
Its not limited to a particular office and i cant work out a pattern (eg it effects some users using the same IE6 version as me). Seems intermittent so i need to identify whats causing this unreliability in the function.
PD69Author Commented:
More info - looks like a zip file wont be corrupted but a .exe will.
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

I use a function that is much like your second code. You say it does not work with larger files. What is going wrong when you do that? I never met such a problem.

Sub WriteFileToBrowser(ByVal sFilePath, ByVal sContentType, ByVal sDisplayName)
    Dim oStream
    Set oStream = Server.CreateObject("ADODB.Stream")
    oStream.Type = 1
    oStream.LoadFromFile sFilePath
    Response.ContentType = sContentType
    Response.AddHeader "Content-Disposition", "filename=" & sDisplayName
    Response.AddHeader "Content-Length", oStream.Size
    Response.BinaryWrite oStream.Read
    Set oStream = Nothing
End Sub
If the user downloads the file and finds that it is corrupt and so downloads the file again, is it possible that the download will be successful?

If it is then it may just be a connectivity issue i.e. the connection is dropping out before the file is completely downloaded.

Can the users who ar experiencing the problem give you a detailed report of what actually happens when downloading i.e. do they use IE and if so, does it actually get to 100% and say that the download is complete in the download dialog box?

PD69Author Commented:
No, fails every time on .exe files, downloads ok but then when the executable is run it just reports an error with the file. Have seen the same thing happen with jpg's where the picture will open but part of the image is mashed up.

Somethings obviously getting into the binary stream and corrupting the file but i cant see why it'd happen for some people and not others and also for certain file types and not others.

sybe - the inability for that function to handle large files is well known and documented by MS and on loads of other sites, its to do with the buffer becoming full and for that reason the file needs chopping into chunks or the buffer periodically cleared. Have done loads of testing with functions like that and it wont work properly. Saying that i've just tested your function and it works fine locally with large files but so does my other function. Its only when put them into production environments and use large files that the errors occur, it'll just report page not found or just prompt you download the .asp file itself then report not found.
Is there any commonality with the users which it fails on i.e. version of IE, are they using IE or another browser?

If the failure isn't intermittent then it should be something that can be tracked down.

Would you like me to try downloading the file and report the result?

PD69Author Commented:
Solved this in the end by altering:

for i = 0 to objStream.size
          i = i + 128000

To ...

for i = 0 to objStream.size
          i = i + 64000

For some reason with a smaller size the corruption doesnt occur (dont ask me why). Cheers for the offer of testing Bill, but solved it the same day but didnt get around to updating this post.
Glad you got it sorted anyways.

PAQed with points refunded (500)

Community Support Moderator

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.