This project has moved and is read-only. For the latest updates, please go here.

Does Docx.SaveAs(stream) work?

Oct 1, 2009 at 2:01 PM

I am trying to save a Docx into HttpResponse.OutputStream, so that the Word document can be downloaded from the server. However, I got the error message "Specified method is not supported" for the line pDoc.SaveAs(Response.OutputStream).

Did I do something wrong here? How am I supposed to use Docx.SaveAs(stream) method?

Thanks for your help!


Oct 2, 2009 at 1:24 AM

I just finished using it in an app. What I've done is copy it to Save it to a memory Stream, and use that to send the byte sequence in Response output:


string filename = "filename.docx";
byte[] contents;

using (MemoryStream ms = new MemoryStream())
     using (DocX document = DocX.Create(filename))
            //do whatever to fill the file


      contents = ms.ToArray();

ResponseHelper.SendContent(contents, filename); //calls with forcedownload = true;


ResonseHelper here is a class I have in a personal framwork, for reuse,  that I use to force downloads,. Looks like this:

public static void SendContent(byte[] fileContent, string outFileName, bool forceDownload)

            if (forceDownload)
                HttpContext.Current.Response.AddHeader("Content-Type", "application/force-download");
            else            // Set the ContentType
                HttpContext.Current.Response.ContentType = ReturnMimeType(outFileName.GetFileExtension()); //Return mime typ returns mime type based on extension.. just a long switch statement with what I need

 // LINE1: Add the file name and attachment, which will force the open/cance/save dialog to show, to the header HttpContext.Current.Response.AddHeader("Content-Disposition", String.Format("attachment; filename={0}", outFileName.Replace(" ", "_"))); // Add the file size into the response header HttpContext.Current.Response.AddHeader("Content-Length", fileContent.Length.ToString()); // Write the file into the response (TransmitFile is for ASP.NET 2.0. In ASP.NET 1.1 you have to use WriteFile instead) HttpContext.Current.Response.OutputStream.Write(fileContent, 0, fileContent.Length); HttpContext.Current.Response.Flush(); // End the response HttpContext.Current.Response.End(); }

Since what you write to the response is a byte[], save the doc to memory (or any stream you prefer), and write the contents of the stream to Response.

Oct 2, 2009 at 11:07 AM

thank you for answering this question. I am sorry that I did not reply sooner. I have been very busy lately with college. I have just started my 4th and final year of "Computer Science & Software Engineering". Unfortunately this means that I am going to have less time to develop DocX over the next few months. I will try and find some time over the weekends.

kind regards and happy coding,
Oct 2, 2009 at 2:16 PM

Thanks for posting your solution here! Appreciate it.