How to open Word Document in c# - Web Application only?

Jaya kumar
Jaya kumar used Ask the Experts™

How to open Word Document in c# - Web Application only?
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Below As Attachement a sample code for that
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace OpenWordDocumentInASPNetApp {
  public partial class _Default : System.Web.UI.Page {
    protected void Page_Load(object sender, EventArgs e) {
      OpenFileStreamAtClient(Server.MapPath("~") + "\\ABC.docx");

    private void OpenFileStreamAtClient(string filepath) {
      System.IO.Stream fileStream = null;

      // Buffer to read 10K bytes in chunk:
      byte[] byteToReadWriteFromToFile = new byte[10001];

      // Length of the file:
      int lengthOfData = 0;

      // Total bytes to read:
      long dataToRead = 0;

      // Identify the file name without whole page path.
      string filename = System.IO.Path.GetFileName(filepath);

      try {
        // Open the file.
        fileStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);

        // Total bytes to read:
        dataToRead = fileStream.Length;
        Response.Buffer = true;
        Response.BufferOutput = false;
        Response.ContentType = "application/vnd.msword.document.12";
        Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
        Response.AddHeader("Content-Length", dataToRead.ToString());

        // Read the bytes.
        while(dataToRead > 0) {
          // Verify that the client is connected.
          if(Response.IsClientConnected) {
            // Read the data in buffer
            lengthOfData = fileStream.Read(byteToReadWriteFromToFile, 0, 10000);

            // Write the data to the current output stream.
            Response.OutputStream.Write(byteToReadWriteFromToFile, 0, lengthOfData);

            // Flush the data to the HTML output.

            byteToReadWriteFromToFile = new byte[10001];
            // Clear the buffer
            dataToRead = dataToRead - lengthOfData;
          else {
            //prevent infinite loop if user disconnects
            dataToRead = -1;

      catch(Exception) {
      finally {
        if(fileStream != null)
          // Close the file.



Open in new window

No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I have recommended this question be closed as follows:

-- Fouad Maidine (https:#a40890991)
-- Dhanasekaran Sengodan (https:#a40891221)

If you feel this question should be closed differently, post an objection and the moderators will review all objections and close it as they feel fit. If no one objects, this question will be closed automatically the way described above.

Experts-Exchange Cleanup Volunteer

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial