Determine file real filetype

Hi

Considering my product receiving lot of different filetypes and I cannot relay on the extension (sometimes it does not exist) to determine the filetype of the file.
Is there an API in aspose total that can tell the file type of the file?
sending the file one by one to all aspose products is not a good option :slight_smile:
Thanks!

@yaird

Currently, there is no single API in Aspose that can detect all file types. You will need to use different Aspose products to identify specific file types. For example, you can use Aspose.Words to check for Word documents, and for Excel files, you can refer to the documentation on detecting file formats and checking if a file is encrypted.

For file types that are not covered by these products, such as images or MSG files, you may need to implement your own logic based on the metadata or MIME type associated with those files.

If you are looking for a more streamlined approach, consider creating a wrapper function that utilizes the relevant Aspose APIs based on the initial checks you perform on the file. This way, you can minimize the number of calls to different APIs while still accurately determining the file type.

For more detailed guidance, you can refer to the following resources:

If you have any further questions or need assistance with specific file types, feel free to ask!

Can you suggest a FAST way in aspose words, cells, ppt and pdf to detect the filetype?

@yaird,

For Aspose.Cells, we already suggested the document(with example code) on how to detect a file format for your reference. This approach is already optimised and is fast. Aspose.Cells provides the FileFormatUtil.DetectFileFormat() static method and some relevant APIs that you can use to parse documents. My colleagues from Aspose.Words, Aspose.PDF and Aspose.Slides teams will assist you on detecting relevant file formats for your reference.

@yaird Aspose.Words.FileFormatUtil.DetectFileFormat method is designed for detecting file formats supported for loading by Aspose.Words.

@yaird,
Unfortunately, Aspose.Total does not provide a single method to determine the type of files. As for PowerPoint documents, you can use Aspose.Slides for .NET like this:

var info = PresentationFactory.Instance.GetPresentationInfo(filePath);
Console.WriteLine("File format: " + info.LoadFormat);

More examples: Examine Presentation|Aspose.Slides Documentation

what about PDF files?

@yair.da

You can use below code snippet with Aspose.PDF to determine whether provided document is a valid PDF document or not:

Document doc = new Document(dataDir + "original.pdf");
Facades.PdfFileInfo info = new Facades.PdfFileInfo();
info.BindPdf(doc);
Console.WriteLine(info.IsPdfFile ? "Format: PDF" : "Unknown");