Babak
Babak

Reputation: 3976

Streaming OpenXML result

By using OpenXML to manipulating a Word document (as a template), the server application saves the new content as a temporary file and then sends it to user to download.

The question is how to make these content ready to download without saving it on the server as a temporary file? Is it possible to save OpenXML result as a byte[] or Stream instead of saving it as a file?

Upvotes: 1

Views: 11345

Answers (3)

Hamid
Hamid

Reputation: 42

  var memoryStream = new MemoryStream();
  document.Clone(memoryStream);

Upvotes: 1

Babak
Babak

Reputation: 3976

Using this page: OpenXML file download without temporary file

I changed my code to this one:

byte[] result = null;
byte[] templateBytes = System.IO.File.ReadAllBytes(wordTemplate);
using (MemoryStream templateStream = new MemoryStream())
{
    templateStream.Write(templateBytes, 0, (int)templateBytes.Length);
    using (WordprocessingDocument doc = WordprocessingDocument.Open(templateStream, true))
    {
        MainDocumentPart mainPart = doc.MainDocumentPart;           
        ...         
        mainPart.Document.Save();
        templateStream.Position = 0;
        using (MemoryStream memoryStream = new MemoryStream())
        {
            templateStream.CopyTo(memoryStream);
            result = memoryStream.ToArray();
        }
    }
}

Upvotes: 3

jn1kk
jn1kk

Reputation: 5112

You can create the WordprocessingDocument and then use the Save() method to save it to a Stream.

http://msdn.microsoft.com/en-us/library/cc882497

Upvotes: 2

Related Questions