• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 594
  • Last Modified:

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.
0
lpenrod
Asked:
lpenrod
  • 10
  • 6
  • 3
2 Solutions
 
ap_sajithCommented:
See if the fix explained in the article sorts out your issue.
http://support.microsoft.com/?kbid=308090

Cheers!
0
 
ap_sajithCommented:
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
 
darkeryuCommented:
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 Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
lpenrodAuthor Commented:
KB did not apply.

Response.Buffer=True did not fix.

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

Any more ideas?
0
 
darkeryuCommented:
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
 
lpenrodAuthor Commented:
It wrote a bunch of jibberish in the browser.
0
 
lpenrodAuthor Commented:
I changed: mime_type="application/save-as"
to: mime_type="application/zip"

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

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

thanks
0
 
lpenrodAuthor Commented:
It wrote a bunch of jibberish in the browser instead of prompting to download the file.
0
 
ap_sajithCommented:
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
 
ap_sajithCommented:
>>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
 
ap_sajithCommented:
Do you need further assistance with this?

Cheers!
0
 
lpenrodAuthor Commented:
Sorry, I have been out of town.  I will try this evening.
0
 
lpenrodAuthor Commented:
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
 
lpenrodAuthor Commented:
The original file is 22,311 bytes.
The "Copy_" file is 22,311 bytes.
The downloaded (corrupt) file is 22,347 bytes.
0
 
lpenrodAuthor Commented:
OK, opened the downloaded file in notepad and I see HTML code before and after the file.

Surely a simple fix...?
0
 
lpenrodAuthor Commented:
Yea!

I added:
      Response.Clear
      Response.Buffer=True
before the line:
      Response.ContentType = sContentType
and now it works!
0
 
lpenrodAuthor Commented:
You were both exremely helpful.  Should I split the points?
0
 
ap_sajithCommented:
Split is fine with me.

Cheers!
0
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.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 10
  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now