[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Files corrupted using BinaryWrite

Posted on 2006-03-30
11
Medium Priority
?
763 Views
Last Modified: 2008-02-01
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
     Response.Clear

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

     ' 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>")
          Response.End
     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>")
          Response.End
     end if
     
     'format strFileName
     if Len( Trim(strDownloadFilename) ) > 0 then
          strDownloadFilename = Trim( strDownloadFilename )
     else
          strDownloadFilename = objFilestream.name
     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
          Response.BinaryWrite(objStream.Read(128000))
          Response.Flush
     next

     ' tidy up
     objFilestream.Close
     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.Open()
      adoStream.LoadFromFile Server.Mappath(SLDownloadFileName)
      If adoStream.Type = 1 Then
            stream = adoStream.Read()
      Else
            stream = adoStream.ReadText()
      End If
      adoStream.Close()
      ' tidy up
      Set adoStream = Nothing
      Response.Clear()
      Response.Buffer = True
      Response.AddHeader "Content-Disposition", "attachment; filename=" & RSFileDownload.Fields.Item("D2DownloadPath").Value
      Response.ContentType = FileContentType
      Response.BinaryWrite stream
      Response.Flush

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

Thanks.
0
Comment
Question by:PD69
10 Comments
 
LVL 5

Expert Comment

by:Willibob
ID: 16332348
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.

Bill
0
 
LVL 1

Author Comment

by:PD69
ID: 16332470
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.
0
 
LVL 1

Author Comment

by:PD69
ID: 16336974
More info - looks like a zip file wont be corrupted but a .exe will.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 28

Expert Comment

by:sybe
ID: 16339920
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.Open
    oStream.LoadFromFile sFilePath
    Response.ContentType = sContentType
    Response.AddHeader "Content-Disposition", "filename=" & sDisplayName
    Response.AddHeader "Content-Length", oStream.Size
    Response.BinaryWrite oStream.Read
    oStream.Close
    Set oStream = Nothing
End Sub
0
 
LVL 5

Expert Comment

by:Willibob
ID: 16340435
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?

Bill
0
 
LVL 1

Author Comment

by:PD69
ID: 16341692
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.
0
 
LVL 5

Expert Comment

by:Willibob
ID: 16341949
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?

Bill
0
 
LVL 1

Author Comment

by:PD69
ID: 16506613
Solved this in the end by altering:

for i = 0 to objStream.size
          i = i + 128000
          Response.BinaryWrite(objStream.Read(128000))
          Response.Flush

To ...

for i = 0 to objStream.size
          i = i + 64000
          Response.BinaryWrite(objStream.Read(64000))
          Response.Flush

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.
0
 
LVL 5

Expert Comment

by:Willibob
ID: 16506669
Glad you got it sorted anyways.

Bill
0
 

Accepted Solution

by:
GranMod earned 0 total points
ID: 16727298
PAQed with points refunded (500)

GranMod
Community Support Moderator
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
Screencast - Getting to Know the Pipeline
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month20 days, 4 hours left to enroll

873 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