Open docx from asp.net
I am trying to open a docx file with the following piece of code.
Response.AddHeader("content-disposition", "inline;filename=DisplayFileName.docx");
Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
Response.WriteFile(fullpath);
Response.Flush();
Response.End();
The problem is that it throws up an error that file is corrupt. Is the above content type correct? Other file types are fine when the appropriate content type is given. The problem is only with docx. Please help. Its urgent...
John GlennPosted Mar 9, 2012, 5:45 AM
try this code to ASP.NET export to Word.
ComponentInfo.SetLicense("FREE-LIMITED-KEY");
// Create a new empty document.
DocumentModel document = new DocumentModel();
// Add document content.
document.Sections.Add(new Section(document, new Paragraph(document, "Hello World!")));
// Microsoft Packaging API cannot write directly to Response.OutputStream.
// Therefore we use temporary MemoryStream.
using (MemoryStream documentStream = new MemoryStream())
{
document.Save(documentStream, SaveOptions.DocxDefault);
// Stream file to browser.
Response.Clear();
Response.ContentType = "application/vnd.openxmlformats";
Response.AddHeader("Content-Disposition", "attachment; filename=Document.docx");
documentStream.WriteTo(Response.OutputStream);
Response.End();
}
This code uses this C# / VB.NET Word library.
Sandhya ChandrasekharanPosted Oct 14, 2010, 1:44 AM
Suthish NairPosted Oct 13, 2010, 4:07 PM
Posted Oct 13, 2010, 10:17 AM
http://www.vistax64.com/net-general/171396-how-open-docx-file-asp-net-application.html
http://forums.asp.net/t/1416640.aspx
http://stackoverflow.com/questions/3162989/docx-file-doesnt-open-in-browser-with-content-disposition-inline-in-ie-8