Aspose seems to be my last resort to solve the error I’m receiving during my Document Upload process.
Because I’m using SoftArtisans.FileUp software to upload documents I’m not able to test if the document exists or is a valid document before I open it with the ComHelper Class. The error is as follows:
ASP Error Information:
Page: /DocSystem/ProcessUploadDoc.asp
File: /DocSystem/ProcessUploadDoc.asp [95]
Source:
Category: Aspose.Words
Description: Unknown file format.
Number: 0x80131500 (-2146233088)
Line 95 : “Set currDoc = comHelper.Open(sUploadedFile)”
Is there a way to test the document format to be valid before I open it, or can I test a return code from the comHelper.Open process to see if it failed?
You can use Document.DetectFileFormat method for this. It will return LoadFormat.Unknown if the provided document cannot be opened in Aspose.Words.
Hope this helps,
Thank you for your reply, but do you have a working example of how Document.DetectFileFormat is used? I’m a little confused because how do you use this method unless you first open the document. And opening the document is where I’m having the problem to begin with.
Hi,
The example is simple:
if (Document.DetectFileFormat(fileName) != LoadFormat.Unknown)
{
// ... document is valid.
}
The DetectFileFormat method is static so the document is not open (i.e. the Document class is not instantiated).
Thanks.
Is there a way to make it work with the ComHelper Class?
Oh you are using COM, sorry for not noticing. Well it’s possible to call static .NET methods from COM, you just need to create a wrapping class in .NET, instantiate its object and invoke its method (delegating to DetectFileFormat) from COM. You can find more information on the Web, for example here:
http://www.dotnet247.com/247reference/msgs/52/262989.aspx
Hope this helps.
I now see they use reflection there. I think this is excessive for this simple case.
I’ve found our past discussion where Vladimir composed a sample wrapper for you.
I think all you need to do is add a method to that wrapper that passes the name of the document to Document.DetectFileFormat and that’s it.
Please let me know if it works.
I believe you’re right, however I’m currently not able to do what you’ve suggested. I’m working another assignment, and it would take too much time away from that assignment at the moment.
I have modified the wrapper for you, see attached.
However, after consulting with the rest of the team, I concluded that DetectFileFormat might not be the complete solution. It only performs some simple detection of the format based on the document’s header and does not guarantee that there won’t be a failure during reading. Given that, wouldn’t it be better to handle the error as it is appropriate in COM (On Error Goto or something)?