Download OpenXML file without a temporary file

Is there a way to provide a download on an ASP.Net page for a newly created OpenXML (docx) file without saving it in a temporary folder?

On MSDN I just found a tutorial on using a temporary file, but I thought about using WordprocessingDocument.MainDocumentPart.GetStream() and directly writing a stream from.

+6
source share
2 answers

When creating a document, use MemoryStream as the backup storage. Then create and close the document as usual and transfer the contents of the memory stream to the client.

 using(var stream = new MemoryStream()) { using(var doc = WordprocessingDocument.Create(stream, WordprocessingDocumentType.Document, true) { ... } stream.Position = 0; stream.CopyTo(Response.OutputStream); } 

Do not just grab MainDocumentPart , because, as the name implies, this is just one part of the document package, not all.

You will also need to set response headers for the type of content and its location.

+12
source

Stream.CopyTo () in .NET 4.0 can help you here.

 WordprocessingDocument.MainDocumentPart.GetStream().CopyTo(Response.OutputStream); 

You still need to set headers for the MIME type, content, etc.

+1
source

Source: https://habr.com/ru/post/886198/


All Articles