Excel file generated: Corrupt file

I was generating an Excel file via a web app.  Everything seemed to be working well, until I touched the code.  I made one change, to set the download filename.  Then I started getting an error from Excel:

  "Corrupt file"

As you can see, that is not much information for troubleshooting. However, when I saved the file and opened it from my desktop, Excel gave me more information about the problem:

  Problems During Load
    Problems came up in the following areas during load:
      Workbook setting

    This file cannot be opened because of errors.  Errors are listed in:
    C:\users\[myaccount]\AppData\Local\Microsoft\Windows\Temporary Internet Files\Content.MSO\924EDDF.log

When I open the error log file, it said that the XML is incomplete.

I opened the xml file and I see that, for some reason, the last 20 bytes (more/less) have been truncated. I don’t see how my change would cause that, so I thought about other possible causes (which might have been there before I touched it).

First (and most-common cause) that I checked, was to make sure that I was flushing the outgoing stream.  Yes, that was already done.

Here is the (flawed) code: (see if you can spot the error, before I reveal it, below)

  Response.Buffer = True
  Response.Charset = ""
  Response.Cache.SetCacheability(System.Web.HttpCacheability.Private) 'http://stackoverflow.com/a/8404198/283895
  Response.AddHeader("content-disposition", ("attachment; filename=" + exportFileName + ".xml"))
  Response.AddHeader("Content-Length", outfilexml.Length.ToString())
  Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"

The problem turned-out to be the line that sets “Content-Length”.  Apparently, this specifies the content length of the entire HTTP response and not just the file.  So, since I was only sending a content length based on the length of the file,  the browser was (sometimes) discarding the HTTP content after the specified content length. 

As you can see, I have about 200 bytes of other header information that I was adding (Plus the web server was probably adding cookies and other header stuff) before I generated the file. So, the browser stopped downloading after it got all of the content (length) that I had specified.

Once I removed the code that set the Content-Length, everything started working properly. The browser just downloaded all of the content.  No more file truncation.

I thought I would share, in case anyone else ran into this weird error.


About Tim Golisch

I'm a geek. I do geeky things.
This entry was posted in ASP.NET, Errors, Programming and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s