title: How do I return a MemoryStream docx file MVC?
body: <p>I have a docx file that I would like to return after I make edits. I have the following code...</p>
  
  <pre><code>object useFile = Server.MapPath("~/Documents/File.docx");
  object saveFile = Server.MapPath("~/Documents/savedFile.docx");
  MemoryStream newDoc = repo.ChangeFile(useFile, saveFile);
  return File(newDoc.GetBuffer().ToArray(), "application/docx", Server.UrlEncode("NewFile.docx"));
  </code></pre>
  
  <p>The file seems fine, but I am getting error messages ("the file being corrupt" and another stating "Word found unreadable content. If you trust the source click Yes"). Any ideas?</p>
  
  <p>Thanks in advance</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>This is the ChangeFile in my Model... </p>
  
  <pre><code>    public MemoryStream ChangeFile(object useFile, object saveFile)
      {
          byte[] byteArray = File.ReadAllBytes(useFile.ToString());
          using (MemoryStream ms = new MemoryStream())
          {
              ms.Write(byteArray, 0, (int)byteArray.Length);
              using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(ms, true))
              {                    
                  string documentText;
                  using (StreamReader reader = new StreamReader(wordDoc.MainDocumentPart.GetStream()))
                  {
                      documentText = reader.ReadToEnd();
                  }
  
                  documentText = documentText.Replace("##date##", DateTime.Today.ToShortDateString());
                  using (StreamWriter writer = new StreamWriter(wordDoc.MainDocumentPart.GetStream(FileMode.Create)))
                  {
                      writer.Write(documentText);
                  }
              }
              File.WriteAllBytes(saveFile.ToString(), ms.ToArray());
              return ms;
          }
      }
  </code></pre>