title: save the document created by docX into response and send it to user for downloading
body: <p>I am trying to use the amazing <a href="http://docx.codeplex.com/" rel="nofollow noreferrer">DocX library on codeplex</a> to create a word document.</p>
  
  <p>when the user clicks a button, the document is created and I want to be able to send it to the user immediately via response.. I am doing something similar to this now: </p>
  
  <p><strong>Edited code based on suggestions</strong></p>
  
  <pre><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();
  }
  </code></pre>
  
  <p>I have tried a few variations of this.. but I am not able to achieve what I want.. <a href="https://stackoverflow.com/a/7634999/363674">Looking at this answer</a> I can possibly save the document on the server and open with io stream.. but I want to avoid that extra step (and then I need to delete the file too)</p>
  
  <p>I don't see the point of creating a file for few milli seconds.. there has to be a way to save the contents and send them to response stream.. right?</p>
  
  <p>How'd I go about it? 
  thanks..</p>
  
  <p>EDIT: my current code either throws up cannot open file (Access denied) error If I am using file stream, OR downloads an empty document file without any content (sometimes, type of response is written to document)</p>
  
  <p><hr>
  This code gets me an MS word document with <code>System.IO.MemoryStream</code> as it's content..</p>
  
  <hr>
  
  <p>Okay, here is the final working solution: </p>
  
  <p>For some reason, DocX library doesn't want to save to <code>Response.OutputStream</code> directly, so I had to save it to memory stream and write the memory stream to response, like <a href="https://stackoverflow.com/users/282740/neil-moss">Neil</a> &amp; <a href="https://stackoverflow.com/users/572644/daniel-hilgarth">Daniel</a> suggested. Here's what worked for me: </p>
  
  <pre><code>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();
  </code></pre>