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.

Tue Jan 11, 2022 10:14 am

I'm trying to generate a Word document using Spire.Doc and download it. I can download the document, but it is empty. Does anyone have any idea why that is and can help me? This is my code:
Document doc = new Document();
Paragraph paragraph = doc.AddSection().AddParagraph();
paragraph.AppendText("Hello World!");
doc.SaveToFile("Example.doc", FileFormat.Doc);
//Saves the Word document to MemoryStream
MemoryStream stream = new MemoryStream();
stream.Position = 0;


//Download Word document in the browser
return File(stream, "application/msword", "Example.doc");

georgschm86
 
Posts: 1
Joined: Tue Jan 11, 2022 10:05 am

Wed Jan 12, 2022 6:42 am

Hello,

Thank you for your inquiry.
After checking the code you provided, I found that you are not writing data to "stream", which results in the downloaded Word file being an empty file. I modified your code. Please test it on your side and tell us your testing result.
Code: Select all
Document doc = new Document();
Paragraph paragraph = doc.AddSection().AddParagraph();
paragraph.AppendText("Hello World!");
doc.SaveToFile("Example.doc", FileFormat.Doc);
//Saves the Word document to MemoryStream
MemoryStream stream = new MemoryStream();
stream.Position = 0;
doc.SaveToStream(stream, FileFormat.Doc);

//Download Word document in the browser
return File(stream, "application/msword", "Example.doc");

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1643
Joined: Wed Apr 07, 2021 2:50 am

Tue Jan 18, 2022 8:57 am

Hello,

Hope you are doing well!
How is your issue going? Did the code we provided work for you? Any feedback will be greatly appreciated.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1643
Joined: Wed Apr 07, 2021 2:50 am

Return to Spire.Doc