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


How to open Word Document in c# - Web Application only?
Jaya kumarAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Maidine FouadEngineerCommented:
Below As Attachement a sample code for that

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Dhanasekaran SengodanCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.