Link to home
Start Free TrialLog in
Avatar of lpenrod
lpenrod

asked on

File is corrupt when downloaded

I have a folder that contains a zip file.  If I open the file directly, it opens just fine.

I use this code I found here on EE from Sybe:

      sFilePath = "C:\sites\Single44\sam\webroot\secret_folder\Color Particle Logo.zip"
      sContentType = "application/zip"
      sDisplayName = "Test download.zip"
    Dim oStream
    Set oStream = Server.CreateObject("ADODB.Stream")
    oStream.Type = 1
    oStream.Open
    oStream.LoadFromFile sFilePath
    Response.ContentType = sContentType
    Response.AddHeader "Content-Disposition", "attachment; filename=" & sDisplayName
    Response.AddHeader "Content-Length", oStream.Size
    Response.BinaryWrite oStream.Read
    oStream.Close
    Set oStream = Nothing

The file downloads, but won't open ("It does not appear to be a valid archive").

The original and downloaded files are the same size.

Not sure what the problem is.  Lots of people seem to be using this code with lots of success.
Avatar of ap_sajith
ap_sajith

See if the fix explained in the article sorts out your issue.
http://support.microsoft.com/?kbid=308090

Cheers!
ASKER CERTIFIED SOLUTION
Avatar of ap_sajith
ap_sajith

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi:


    sFilePath = "C:\sites\Single44\sam\webroot\secret_folder\Color Particle Logo.zip"
     sContentType = "application/zip"
     sDisplayName = "Test download.zip"
    Dim oStream
    Set oStream = Server.CreateObject("ADODB.Stream")
    oStream.Type = 1
    oStream.Open
    oStream.LoadFromFile sFilePath
    Response.ContentType = sContentType
    Response.AddHeader "Content-Disposition", "attachment; filename=" & sDisplayName

   ' Response.AddHeader "Content-Length", oStream.Size  <----   remark this line
    Response.BinaryWrite oStream.Read
    oStream.Close
    Set oStream = Nothing
Avatar of lpenrod

ASKER

KB did not apply.

Response.Buffer=True did not fix.

Reming Response.AddHeader "Content-Length", oStream.Size did not fix.

Any more ideas?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lpenrod

ASKER

It wrote a bunch of jibberish in the browser.
Avatar of lpenrod

ASKER

I changed: mime_type="application/save-as"
to: mime_type="application/zip"

Now it prompts me to save, but it is still corrupt.
Hi:

if you change the mime_type to /save-as,still have error?

thanks
Avatar of lpenrod

ASKER

It wrote a bunch of jibberish in the browser instead of prompting to download the file.
Just to ensure that the problem lies with the setting of the HTTP headers, please try the code below..

 sFilePath = "C:\sites\Single44\sam\webroot\secret_folder\Color Particle Logo.zip"
 sFilePath1 = "C:\sites\Single44\sam\webroot\secret_folder\Copy_Color Particle Logo.zip"

     sContentType = "application/zip"
     sDisplayName = "Test download.zip"
    Dim oStream
    Set oStream = Server.CreateObject("ADODB.Stream")
    oStream.Type = 1
    oStream.Open
    oStream.LoadFromFile sFilePath
    Response.ContentType = sContentType
    Response.AddHeader "Content-Disposition", "attachment; filename='" & sDisplayName & "'"
    'Response.AddHeader "Content-Length", oStream.Size
    Call oStream.SaveToFile(sFilePath1)
    Response.BinaryWrite oStream.Read
    oStream.Close
    Set oStream = Nothing

Check whether the code is creating a copy of the downloaded file as well as prompting a file download. Try opeing the copy of the zip file just created and see if thats corrupt.

Cheers!

>>It wrote a bunch of jibberish in the browser instead of prompting to download the file.

Try opeing it in a new browser window. Sometimes, after errors have occured, the download code writes jibberish on to the browser :)

Cheers!
Do you need further assistance with this?

Cheers!
Avatar of lpenrod

ASKER

Sorry, I have been out of town.  I will try this evening.
Avatar of lpenrod

ASKER

My appologies for taking so long to get back to this...

The file that downloads is corrupt.  
The file that is copied in not corrupt, it is fine.
Avatar of lpenrod

ASKER

The original file is 22,311 bytes.
The "Copy_" file is 22,311 bytes.
The downloaded (corrupt) file is 22,347 bytes.
Avatar of lpenrod

ASKER

OK, opened the downloaded file in notepad and I see HTML code before and after the file.

Surely a simple fix...?
Avatar of lpenrod

ASKER

Yea!

I added:
      Response.Clear
      Response.Buffer=True
before the line:
      Response.ContentType = sContentType
and now it works!
Avatar of lpenrod

ASKER

You were both exremely helpful.  Should I split the points?
Split is fine with me.

Cheers!