Hey There,
I am getting file corruption exception, attaching my code below -
using (MemoryStream inStream = Base64ToStream(pdfConverter.Base64String))
{
LoadOptions loadOptions = new LoadOptions();
loadOptions.LoadFormat = LoadFormat.Docx; // Assuming the input is a DOCX file, change as needed
Document doc = new Document(inStream, loadOptions);
}
I am getting it on Document doc = new Document(inStream, loadOptions);
I tried file path instead of inStream as well but results are same.
@Maurya1337
It seems you are encountering a FileCorruptedException when trying to load a document using Aspose.Words. This exception typically indicates that the file you are attempting to open is not in a valid format or is corrupted.
Here are a few suggestions to troubleshoot and resolve the issue:
-
Verify Input Format: Ensure that the input you are providing (in your case, the Base64 string) is indeed a valid DOCX file. If the Base64 string is not correctly formatted or represents a different file type, it will lead to this exception.
-
Check Base64 Conversion: Make sure that the method Base64ToStream is correctly converting the Base64 string back to a stream. You can test this by saving the stream to a file and trying to open it with a compatible application (like Microsoft Word) to see if it opens without errors.
-
Use File Path: Since you mentioned trying a file path as well, ensure that the file at that path is accessible and not corrupted. You can also try opening the file directly in a word processor to confirm its integrity.
-
LoadOptions: Verify that the LoadOptions you are using is appropriate for the file type you are attempting to load. If you are loading a DOCX file, ensure that the LoadFormat is set correctly.
If you continue to experience issues, consider checking the documentation for more details on handling file formats and exceptions, or reach out to the Aspose community for further assistance.
If you need more specific guidance, please provide additional details about the Base64 string or the file you are working with.
Sources:
[1]: FileCorruptedException class | Aspose.Words for Python
File path is accessible, I am able to open that docx file locally without any error.
I tried without passing loadOptions as well, still getting same error.
@Maurya1337 Could you please attach the problematic input document here for testing? We will check the issue and provide you more information.
test.docx (15.2 KB)
test_document.docx (35.8 KB)
I tried these 2, even a blank file also throws same exception
@Maurya1337 Thank you for additional information. Your documents works fine on my side. Could you please make sure data that comes from Base64ToStream(pdfConverter.Base64String) method contains a valid DOCX document. Also, make sure the stream position is at the beginning of the stream (Aspose.Words reads the stream from it’s current position). Please try modifying the code like this:
using (MemoryStream inStream = Base64ToStream(pdfConverter.Base64String))
{
// Set stream position to the beginning
inStream.Position = 0;
LoadOptions loadOptions = new LoadOptions();
loadOptions.LoadFormat = LoadFormat.Docx; // Assuming the input is a DOCX file, change as needed
Document doc = new Document(inStream, loadOptions);
}
@alexey.noskov I tried setting position of stream to 0 but still didn’t worked. I tried by passing direct path of the file, getting same error 
Document doc = new Document("C:/Temp/test.docx");
@Maurya1337 Unfortunately, I cannot reproduce the problem on my side. I have tested both of your documents and both work fine on my side with the latest 25.6 version of Aspose.Words.