Microsoft Office Documents

  • Thread starter Thread starter Shogun
  • Start date Start date
S

Shogun

Does anyone know of a way to programmatically determine if a file is a valid
Office document? Our application allows users to upload files via the web,
but restricts them to particular file types. Although we can look at the
mime-type or the file extension, it doesn't really tell us if the file is
valid or not. Our web server won't have office installed, so we need a way to
validate that the file really is what they say it is.

Thanks
 
Does anyone know of a way to programmatically determine if a file is a valid
Office document? Our application allows users to upload files via the web,
but restricts them to particular file types. Although we can look at the
mime-type or the file extension, it doesn't really tell us if the file is
valid or not. Our web server won't have office installed, so we need a way to
validate that the file really is what they say it is.

Thanks

If you open an Office 97-2003 document as a binary file, the first 16 bytes have
the following hex values:

D0 CF 11 E0 A1 B1 1A E1 00 00 00 00 00 00 00 00

Office 2007 (XML) files are actually zip files, so like all zip files their
first four bytes have the hex values

50 4B 03 04

From there you'd have to examine what "files" are in the zip container; if they
include folders named _rels and docProps, it's probably an Office file. If
you're programming your server-side application in a .Net 3.0 environment, there
are classes in the System.IO.Packaging namespace to deal with Office 2007 files;
see http://msdn2.microsoft.com/en-us/library/aa982683.aspx.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top