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");
Here is a working example using .NET Core 3.1 and FreeSpire.Doc v9.9.7
[HttpGet]
public IActionResult DownloadWordDoc()
{
//Create the document object;
Document doc = new Document();
//Create a paragraph and add some text
var paragraph = doc.AddSection().AddParagraph();
paragraph.AppendText("Hello World!");
//Create and save the document to a memory stream
var ms = new MemoryStream();
doc.SaveToStream(ms, FileFormat.Docx);
ms.Seek(0, SeekOrigin.Begin);
//You may want to make this MIME type string into a constant
var fsr = new FileStreamResult(ms, "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
fsr.FileDownloadName = "Example.docx";
return fsr;
}
You may also need the following using statements, but I'm presuming you already have them.
using System.IO;
using Spire.Doc;