ASP Q&A: Dealing with Corrupt Downloaded .docx Files From an ASP Site | CodeGuru

ASP Q&A: Dealing with Corrupt Downloaded .docx Files From an ASP Site

This is the first in a series of tips being posted based on the most read posts in one of our ASP Forums. Problem / Question: When .docx files are downloaded from an ASP page, they can give an error when opened in Word. The error is: “The Open Office XML File cannot be opened […]

Written By
CodeGuru Staff
CodeGuru Staff
Sep 16, 2016
2 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

This is the first in a series of tips being posted based on the most read posts in one of our ASP Forums.

Problem / Question:

When .docx files are downloaded from an ASP page, they can give an error when opened in Word. The error is:

The Open Office XML File cannot be opened because there are problems with the contents. (Details: The file is corrupt and cannot be opened)“.

The files themselves are fine, because they can be opened normally accessed via other means. The ASP download page works fine with other file types (doc, pdf, etc.); however, the same problem occurs for other Office (2007) formats. It appears that something is incorrect with the ASP code with respect to Office file formats; however, I cannot identify the problem. Below is the relevant code snippet.

Code:

 Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
             Set objFile = objFSO.GetFile(strAbsFile)
 Response.Clear
 Response.AddHeader "Content-Disposition", "attachment; filename=" & objFile.Name
 Response.AddHeader "Content-Length", objFile.Size
 Response.ContentType = "application/octet-stream"
 Set objStream = Server.CreateObject("ADODB.Stream")
 objStream.Open
 objStream.Type = 1
 Response.CharSet = "UTF-8"
 objStream.LoadFromFile(strAbsFile)
 Response.BinaryWrite(objStream.Read)

Any help or insight would be appreciated.

Solution:

The problem was that my ASP download page continued to write output *after* the Response.BinaryWrite command. Removing the later content, or adding a Response.End afterward, resolves the issue.

Many file formats can handle extraneous trailing data. For example, JPG images have the number of bytes in the following data encoded in the header to the image (invisible to you), so extra bytes don’t matter. XML, however, is *VERY* fussy and does *NOT* allow *ANYTHING* that is not properly enclosed in XML tags.

Based on posts by forum members Adamroth and answer by Bill Wilkerson

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.