happysmile
happysmile

Reputation: 7777

how to open an word document on click of link button

i have an link button in my gridview once user click that link button i need to open an a word document(the path where the document is stored in server would be like this c:/abc/doc/abc1.doc) so now i should all ow the user to download that document for viewing it.

how to get it achived thank you

Upvotes: 0

Views: 19057

Answers (3)

epotter
epotter

Reputation: 7799

You should look at useing the TransmitFile method instead of the WriteFile method. For what you are doing, it is more efficient.

protected void btnPurchaseOrderOpen_Click(object sender, EventArgs e)
{
    Response.Clear();
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;filename=one.pdf");
    Response.TransmitFile(@"c:\test\one.pdf");
    Response.End();
}

Upvotes: 1

ACP
ACP

Reputation: 35268

Hai,

Add a reference to word object library to your project. Then try this

 Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
    app.Visible = true;
    object visible = true;
    object fileName = @"C:\temp\sample.doc";
    object optional = System.Type.Missing;
    Microsoft.Office.Interop.Word.Document doc = app.Documents.Open(ref fileName, ref optional, ref 
        optional, ref optional, ref optional, ref optional, ref optional, ref 
        optional, ref optional, ref optional,ref optional,ref optional,ref optional,ref optional,ref optional,ref optional);

If you want web browser thing just have a look at saar's answer

Upvotes: 0

Saar
Saar

Reputation: 8474

asp.net, that means you want to open document in web browswer.

here is simple snippet

    string fPath = @"c:/abc/doc/abc1.doc";
    FileInfo myDoc = new FileInfo(fPath);

    Response.Clear();
    Response.ContentType = "Application/msword";
    Response.AddHeader("content-disposition", "attachment;filename=" + myDoc.Name);
    Response.AddHeader("Content-Length", myDoc.Length.ToString());
    Response.ContentType = "application/octet-stream";

    Response.WriteFile(myDoc.FullName);

    Response.End();

Upvotes: 0

Related Questions