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...
Open docx from asp.net
4 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Former memberPosted Oct 13, 2010, 6:17 AM EDT
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
Suthish NairRank #93 · 22.6K pointsPosted Oct 13, 2010, 12:07 PM EDT
string file = "DisplayFileName.docx"; Response.Clear(); Response.ContentType = "application/msword"; Response.AppendHeader("Content-Disposition", "attachment;filename=" + file); Response.WriteFile(file); Response.End(); Response.Flush();Sandhya Chandrasekharan6 pointsPosted Oct 13, 2010, 9:44 PM EDT
John Glenn26 pointsPosted Mar 9, 2012, 12:45 AM EST
try this code to ASP.NET export to Word.
// Use the component in free mode. 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.