Note that there are some explanatory texts on larger screens.

plurals
  1. POServing a word document generated using Open XML from an asp.net webpage
    text
    copied!<p>I'm trying to serve a word document (docx) from an asp.net page using the following code</p> <pre><code> protected void Page_Load(object sender, EventArgs e) { MemoryStream document = new MemoryStream(); using (WordprocessingDocument wordDoc = WordprocessingDocument.Create(document, WordprocessingDocumentType.Document)) { MainDocumentPart mainPart = wordDoc.AddMainDocumentPart(); SetMainDocumentContent(mainPart); } string fileName = "MsWordSample.docx"; Response.AppendHeader("Content-Type", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"); Response.AppendHeader("Content-disposition", "attachment; filename=" + fileName); Response.OutputStream.Write(document.ToArray(), 0, document.ToArray().Length); } public static void SetMainDocumentContent(MainDocumentPart part) { const string docXml = @"&lt;?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes""?&gt; &lt;w:document xmlns:w=""http://schemas.openxmlformats.org/wordprocessingml/2006/main""&gt; &lt;w:body&gt; &lt;w:p&gt; &lt;w:r&gt; &lt;w:t&gt;Hello world!&lt;/w:t&gt; &lt;/w:r&gt; &lt;/w:p&gt; &lt;/w:body&gt; &lt;/w:document&gt;"; using (Stream stream = part.GetStream()) { byte[] buf = (new UTF8Encoding()).GetBytes(docXml); stream.Write(buf, 0, buf.Length); } } </code></pre> <p>but I get the following message when trying to open the file: "the file {file name} cannot be opened because there are problems with the content"</p> <p>I then get an option to recover the document which actually fixes the document.</p> <p>Am I leaving something out?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload