Hi All,

I have a section of my site that allows users to upload and download files.

For security issues, We use a com object to take the file as a binary stream and store it on our network off of the web server (under a different randomly generated file name).

The same com object is used to stream the file back to the user if they open the link to it so they never know where the actual file is stored.

Everything was fine with this until we upgraded to Office 2010 now docx and xlsx files are uploading fine but when they are opened via the web site, a dialog appears telling the user the file has unreadible content and asking if they wish to recover the document.
After saying yes to this the file opens perfectly. Other files .doc .xls .csv .txt etc are all unaffected.

I can open the files directly from the network folder with no issue so I think it is where I am streaming it back to the users browser that is the issue (I'm using VB.net):

sub OpenFile(byref FilePath as string, byref FileName as string)
dim objDocument As Object = CreateObject("DocumentHandler")
dim DocStream() as Byte
If objDocument.CheckDocumentExists(FilePath) then
   'fetch document from network as a byte array
   DocStream = objDocument.FetchDocument(Server.UrlDecode(FilePath))
   'open stream in new container by file name
   Response.Clear()
   Response.ClearHeaders()
   Response.AppendHeader("Content-Disposition", "Attachment; Filename=" & FileName)
   'Write the file directly to the HTTP output stream...
   Response.BinaryWrite(DocStream)
   Response.End()
end if
end sub

If anybody has any idea what is wrong that would be great as just now I have users saying I have corrupted their files.

Anyone???

Hi All,
I have a solution so I thought I'd post in case anyone else has problems with Office 2007 and 2010 files.

Instead of

Response.BinaryWrite(DocStream)

use

Response.OutputStream.Write(DocStream, 0, DocStream.Length-1)

It has been explained to me that because the Office 2010 & 2007 files are now zipped up XML files they can be really fussy about text data appearing after the tags. So you have to be precise about the length of binary data you are sending down to the client. Other file types just get what they need and ignore anything after that but the newer Office documents consider anything extra as incorrectly formed xml code even if it is a zero byte. Which when you think about it makes sense.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.