Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

File is corrupt when downloaded

Posted on 2006-06-27
21
Medium Priority
?
591 Views
Last Modified: 2007-12-19
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.
0
Comment
Question by:lpenrod
[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
  • 10
  • 6
  • 3
21 Comments
 
LVL 21

Expert Comment

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

Cheers!
0
 
LVL 21

Accepted Solution

by:
ap_sajith earned 1000 total points
ID: 16998907
As a side note, you may want to change this line
Response.AddHeader "Content-Disposition", "attachment; filename=" & sDisplayName
to
Response.AddHeader "Content-Disposition", "attachment; filename=""" & sDisplayName & """"

I had previously faced some issues in non-IE browsers and the above change fixed the issue.

Also, try and add the following line at the top of your asp page.
<%
Response.Buffer=True

Cheers!
0
 
LVL 6

Expert Comment

by:darkeryu
ID: 17002477
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
0
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.

 
LVL 3

Author Comment

by:lpenrod
ID: 17003619
KB did not apply.

Response.Buffer=True did not fix.

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

Any more ideas?
0
 
LVL 6

Assisted Solution

by:darkeryu
darkeryu earned 1000 total points
ID: 17008344
Hi:

Sorry, write a sample for you....

<%
  snfile = "C:\sites\Single44\sam\webroot\secret_folder\Color Particle Logo.zip"
  set fs=server.CreateObject("scripting.filesystemobject")
  if fs.FileExists(server.MapPath(snfile))=true then
    set objfile=fs.GetFile(server.MapPath(snfile))
    size=objfile.size
    fname=objfile.name
    mime_type="application/save-as"
    Response.AddHeader "Expires:",0
    Response.ContentType=mime_type
    Response.AddHeader "Content-Disposition","attachment; filename="&fname
    set objStream =server.CreateObject("ADODB.Stream")
    objStream.Open
    objStream.Type=1
    objStream.LoadFromFile server.MapPath(snfile)
    Response.BinaryWrite objStream.Read
  else
    Response.Write "File not found!"
    Response.End
  end if
 
%>
0
 
LVL 3

Author Comment

by:lpenrod
ID: 17010857
It wrote a bunch of jibberish in the browser.
0
 
LVL 3

Author Comment

by:lpenrod
ID: 17013672
I changed: mime_type="application/save-as"
to: mime_type="application/zip"

Now it prompts me to save, but it is still corrupt.
0
 
LVL 6

Expert Comment

by:darkeryu
ID: 17014887
Hi:

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

thanks
0
 
LVL 3

Author Comment

by:lpenrod
ID: 17018285
It wrote a bunch of jibberish in the browser instead of prompting to download the file.
0
 
LVL 21

Expert Comment

by:ap_sajith
ID: 17022272
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!

0
 
LVL 21

Expert Comment

by:ap_sajith
ID: 17022274
>>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!
0
 
LVL 21

Expert Comment

by:ap_sajith
ID: 17067121
Do you need further assistance with this?

Cheers!
0
 
LVL 3

Author Comment

by:lpenrod
ID: 17076977
Sorry, I have been out of town.  I will try this evening.
0
 
LVL 3

Author Comment

by:lpenrod
ID: 17219954
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.
0
 
LVL 3

Author Comment

by:lpenrod
ID: 17229079
The original file is 22,311 bytes.
The "Copy_" file is 22,311 bytes.
The downloaded (corrupt) file is 22,347 bytes.
0
 
LVL 3

Author Comment

by:lpenrod
ID: 17229116
OK, opened the downloaded file in notepad and I see HTML code before and after the file.

Surely a simple fix...?
0
 
LVL 3

Author Comment

by:lpenrod
ID: 17229146
Yea!

I added:
      Response.Clear
      Response.Buffer=True
before the line:
      Response.ContentType = sContentType
and now it works!
0
 
LVL 3

Author Comment

by:lpenrod
ID: 17229229
You were both exremely helpful.  Should I split the points?
0
 
LVL 21

Expert Comment

by:ap_sajith
ID: 17240842
Split is fine with me.

Cheers!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
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 response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

618 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