Hi,
I am trying to save a document to a stream so that it can be used by a HttpResponse object. it all works except the file is corrupted when I try to open it. Please review the code snippet below and advise me what change(s) I need to make.
using (MemoryStream stream = new MemoryStream())
{
_document.Save(stream, SaveFormat.Docx);
stream.Position = 0;
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("Content-Description", "File Transfer");
Response.ContentType = "application/octet-stream";
Response.AddHeader("content-disposition", "attachment; filename=\"" + _documentNameDated + ".docx\"");
Response.AddHeader("Content-Transfer-Encoding", "binary");
Response.Cache.SetExpires(DateTime.Now.AddMinutes(1));
Response.AddHeader("Cache-Control", "must-revalidate");
Response.AddHeader("Pragma", "public");
stream.WriteTo(Response.OutputStream);
}
Response.Flush();
HttpContext.Current.ApplicationInstance.CompleteRequest();
Thanks