Error creating document from memory stream (This is not a structured storage file)

I'm evaluating Aspose.Words and am having some trouble getting it to create Document objects from MemoryStreams.

The document is uploaded, converted to a byte array, and saved to a SQL database in an image column. On retrieval, it's converted back into a byte array and I try to create a Document with this code:

MemoryStream ms = new MemoryStream(ByteArray);

Aspose.Words.Document doc = new Aspose.Words.Document(ms, string.Empty, Aspose.Words.LoadFormat.FormatDocument, string.Empty);

This throws an InvalidOperationException, "This is not a structured storage file."

I've attached the sample document I'm working with. Please advise the correct way to perform this task. Thanks.

Something is done incorrectly when saving the document to database record and back. Please attach the complete code that you use for it. You can also try to save the byte array retrieved from database to a file and attach it also.

Yeah, the document is getting mangled somewhere along the way. See the attachment for a copy that is just binary written after storage.

This is how I'm converting it to a byte array (Template is an HttpPostedFile):

int iDocSize = Template.ContentLength;

Stream DocStream = Template.InputStream;

string strDocType = Template.ContentType;

byte[] DocBytes = new byte[iDocSize];

DocStream.Read(DocBytes, 0, iDocSize);

DocStream.Close();

ByteArray = DocBytes;

Then it's stored to the database like so:

DBCommandWrapper commandWrapper = database.GetStoredProcCommandWrapper("StoreFile");

commandWrapper.AddInParameter("@File", DbType.Binary, ByteArray);

commandWrapper.AddOutParameter("@FileID", DbType.Int64, 8);

database.ExecuteNonQuery(commandWrapper);

Then it's retrieved from the database like so:

DBCommandWrapper commandWrapper = database.GetStoredProcCommandWrapper("GetFile");

commandWrapper.AddInParameter("@FileID", DbType.Int64, FileID);

IDataReader dr = database.ExecuteReader(commandWrapper);

if (dr.Read())

{

ByteArray = (byte[])(dr["File"]);

}

I then attempted to create the Document with the code in my previous posting.

See anything wrong?

Your code looks ok to me. Try to check document data on each stage of the process. At least control the document header bytes which are always D0 CF 11 E0 A1 B1 1A E1 for all Word documents.