Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Fri Dec 16, 2022 9:05 am

Hello,

Our Word .docx template is ok and shows no error when you open it.
We load the template and then save it without filling the file as .docx.
Here is sample code.

var document = new Document(path, FileFormat.Docx);

document.SaveToStream(memoryStream, FileFormat.Docx);

The result file is corrupted and shows error -

"Word found unreadable content in "filename.docx". Do you want to recover the contents of this document? If you trust the source of this document, click Yes."

I put in attachments both files.
Can you please check it?

bitbee
 
Posts: 2
Joined: Thu Feb 09, 2017 8:42 am

Fri Dec 16, 2022 10:26 am

Hello,

Thanks for your inquiry.
I used the latest version of Spire.Doc 10.11.9 to test your scenario, but I didn't reproduce your issue. If you don't use the latest version of Spire.Doc, please update to it and have a test.

Sincerely
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 844
Joined: Tue Mar 08, 2022 2:02 am

Fri Dec 16, 2022 1:13 pm

You are right that if I use the newest Spire.Doc version and I use the method doc.SaveToFile("C:/test.docx"), it works and the file is OK.
But when I use method doc.SaveToStream(memoryStream, FileFormat.Docx) and return it through a browser with ContentType and Headers->

httpContext.Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
httpContext.Response.Headers["Content-Disposition"] = $"attachment;filename=test.docx";

It doesn't work and the file is corrupted.

bitbee
 
Posts: 2
Joined: Thu Feb 09, 2017 8:42 am

Mon Dec 19, 2022 8:52 am

Hello,

Thanks for your feedback.
I used the following code to do a test but I didn't reproduce your issue that the saved file was corrupted. Could you please try my code or send us your project to help us repproduce your issue? Thank you for your assistance in advance.

Code: Select all
  Document doc = new Document();
            doc.LoadFromFile("C:/KapoLetterTemplate.docx");
            MemoryStream memoryStream = new MemoryStream();
            doc.SaveToStream(memoryStream, FileFormat.Docx);
            memoryStream.Position = 0;
            Response.Clear();
            Response.AddHeader("Content-Disposition",
                    String.Format("attachment; filename=\"{0}.{1}\"", "out.docx", "docx"));
            Response.AppendHeader("Content-Length", memoryStream.ToArray().Length.ToString());
            Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
            Response.BinaryWrite(memoryStream.ToArray());
            HttpContext.Current.ApplicationInstance.CompleteRequest();
            Response.Flush();
            Response.End();
            memoryStream.Close();


Sincerely,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Thu Jan 12, 2023 9:54 am

Hello,

Hope you are doing well.
Does the code my colleague Amy provided solve your issue? Could you please give some feedback at your convenience?

Sincerely
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 844
Joined: Tue Mar 08, 2022 2:02 am

Return to Spire.Doc