-2

I try to open a word document with c#.
When I open the document, the page is blocked after.

Here is the code :

HttpContext.Current.Response.Write(temp);
//HttpContext.Current.Response.End();

//HttpContext.Current.Response.Flush();

//HttpContext.Current.Response.Write(sw.ToString());
//HttpContext.Current.Response.clear();
//HttpContext.Current.Response.End();
//HttpContext.Current.Response.SuppressContent = true;
//HttpContext.Current.Response.Close();
//Response.Redirect(Page.Request.Url.AbsolutePath.Substring(0, Page.Request.Url.AbsolutePath.LastIndexOf("/")) + "/PIEditor.aspx?PostID=" + Request.Params["PostID"], true);`
//HttpContext.Current.Response.End();

As you see, I tried different options but without result, the window for opening or saving the document is displayed but I can't click on any buttons the page after. It looks like it is deactivated or stopped.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
user1213375
  • 21
  • 1
  • 5
  • The code you have pasted looks like you are trying to send a message back to a web page, not open a Word document. What are you trying to achieve? – Jeggs Feb 16 '12 at 08:45
  • HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "PIExport.xls")); HttpContext.Current.Response.ContentType = "application/ms-excel"; i try to open document. its opened like i wich but the problem comes after when page is stopped.. thanks. – user1213375 Feb 16 '12 at 08:47
  • you confuse me with the comments, are they comments or you use them ? – Aristos Feb 16 '12 at 08:54
  • i tried them but without result – user1213375 Feb 16 '12 at 08:55
  • they are commented now as you see – user1213375 Feb 16 '12 at 09:01

2 Answers2

1

you can try GemBox.Document component to export Word document from ASP.NET application, if that is what you are trying to do.

Here is a sample C# code that should go in ASPX page code behind:

// 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();
}
GemBox Dev Team
  • 669
  • 5
  • 18
  • Although it's fairly clear from your username that you are associated with the company which makes this software, it would be better in the future if you stated this directly. For instance, "you can try GemBox.Document component (created by the company I work for) ..." – John Saunders Feb 26 '15 at 01:31
  • 1
    Awesome and very well documented software! – benjamingranados Nov 29 '20 at 05:46
0

Try the below code:

//create new MemoryStream object and add PDF file’s content to outStream.
MemoryStream outStream = new MemoryStream();

//specify the duration of time before a page cached on a browser expires
Response.Expires = 0;

//specify the property to buffer the output page
Response.Buffer = true;

//erase any buffered HTML output
Response.ClearContent();

//add a new HTML header and value to the Response sent to the client
Response.AddHeader(“content-disposition”, “inline; filename=” + “output.doc”);

//specify the HTTP content type for Response as Pdf
Response.ContentType = “application/msword”;

//write specified information of current HTTP output to Byte array
Response.BinaryWrite(outStream.ToArray());

//close the output stream
outStream.Close();

//end the processing of the current page to ensure that no other HTML content is sent
Response.End();
Shahzad Latif
  • 1,408
  • 12
  • 29