Save the document created by docX in response and send it to the user for download

I am trying to use the awesome DocX library on codeplex to create a Word document.

when the user clicks the button, the document is created, and I want him to be able to immediately send it to the user through the answer .. I am doing something similar to this now:

Proposal Based Edited Code

using (DocX Report = DocX.Create(string.Format("Report-{0}.doc", DateTime.Now.Ticks))) { Paragraph p = Report.InsertParagraph(); p.Append("Title").FontSize(30).Bold() .Append("Sub title").FontSize(28) .AppendLine() .Append(DateTime.Now.Date) ; MemoryStream ms = new MemoryStream(); Report.SaveAs(ms); Response.Clear(); Response.AddHeader("content-disposition", "attachment; filename=\"" + fileName + ".docx\""); Response.ContentType = "application/msword"; Response.Write(ms); Response.End(); } 

I tried several variations of this ... but I can't achieve what I want. Looking at this answer I can save the document to the server and open it using the io stream .. but I want to avoid this extra step (and then I need to also delete the file)

I see no reason to create a file in a few milliseconds. There should be a way to save the content and send it to the response stream. Correctly?

How could I do this? thanks..

EDIT: my current code either throws up, cannot open the file (Access denied) error If I use a file stream, OR loads an empty document file without any content (sometimes the type of response is written to the document)


This code gets me an MS text document with System.IO.MemoryStream as its contents.

Ok, here is the final working solution:

For some reason, the DocX library does not want to directly store in Response.OutputStream , so I had to save it in the memory stream and write the memory stream in response, for example Neil and Daniel . Here is what worked for me:

 MemoryStream ms = new MemoryStream() Report.SaveAs(ms); Response.Clear(); Response.AddHeader("content-disposition", "attachment; filename=\"" + fileName + ".doc\"); Response.ContentType = "application/msword"; ms.WriteTo(Response.OutputStream); Response.End(); 
+7
source share
4 answers

Try using a MemoryStream instead of a FileStream .

Your current code looks really wrong:

You save the report in the OutputStream current response, and then clear that answer (!)

+2
source

It may be a little late, but I found a way to get this to work with FileStreamResult:

  public FileStreamResult DownloadDocument() { using (DocX document = DocX.Create(@"Test.docx")) { // Insert a new Paragraphs. Paragraph p = document.InsertParagraph(); p.Append("I am ").Append("bold").Bold() .Append(" and I am ") .Append("italic").Italic().Append(".") .AppendLine("I am ") .Append("Arial Black") .Font(new FontFamily("Arial Black")) .Append(" and I am not.") .AppendLine("I am ") .Append("BLUE").Color(Color.Blue) .Append(" and I am") .Append("Red").Color(Color.Red).Append("."); var ms = new MemoryStream(); document.SaveAs(ms); ms.Position = 0; var file = new FileStreamResult(ms, "application/vnd.openxmlformats-officedocument.wordprocessingml.document") { FileDownloadName = string.Format("test_{0}.docx", DateTime.Now.ToString("ddMMyyyyHHmmss")) }; return file; } } 

An important bit sets the position of the storage device back to 0, otherwise it turned out to be at the end, and the file was returned empty.

+4
source

When you execute Report.SaveAs(response.OutputStream); - it already writes the contents of the file to the output stream. You do not need to do Response.Write(response.OutputStream);

So, the code should look like this:

 ... Report.SaveAs(response.OutputStream); Response.AddHeader("content-disposition", "attachment; filename=\"" + fileName + ".doc\""); Response.ContentType = "application/msword"; 
+2
source

I think you have things a little ahead and confused.

First of all, clear the output, then add the headers, then write down the contents.

 Response.Clear(); Response.AddHeader("content-disposition", "attachment; filename=\"" + fileName + ".doc\""); Response.ContentType = "application/msword"; // This writes the document to the output stream. Report.SaveAs(response.OutputStream); Response.End(); 

Also, if your file is a docx file, add .docx, not .doc, to your file name.

+1
source

All Articles