Monday, 18 March 2013

How to Create Download link and download word,pdf file code in asp.net




Description:-
              

in this example we explain that how to give Download Link to User in our Web Application to Download word or pdf file.

To do this you have to First Define an Content Type of the page like

Response.ContentType = "Application/msword";
You have to set the AppenHeader also to which type of File you are giving Downloading offer.

Response.AppendHeader("Content-Disposition", "attachment; filename=Test.doc");

Look at this we Define the Content Type is Word and and also Define the AppendHeader so we provide Downloading Facility for Word File.


In which we provide to Link one is for WordFile Download and second  Link is Pdf File Download.when click on the wordFile link then Download Dialog Box is appear with WordFile same is apply for Pdf.
 

                




Down.aspx:-

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Down.aspx.cs" Inherits="Down" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:linkbutton id="LinkButtonDownloadDoc" runat="server" text="Download WORD" style="color: Maroon;
            font-weight: bold;" onclick="LinkButtonDownload_Click" />
    </div>
    <div>
        &nbsp;</div>
    <div>
        <asp:linkbutton id="LinkButtonDownloadPdf" runat="server" text="Download PDF" style="color: Navy;
            font-weight: bold;" onclick="LinkButtonDownloadPdf_Click" /> 
    </div>
    </form>
</body>
</html>




Down.aspx.cs:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Net;

public partial class Down : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void LinkButtonDownload_Click(object sender, EventArgs e)
    {
        Response.ContentType = "Application/msword";
        Response.AppendHeader("Content-Disposition", "attachment; filename=Test.doc");
        Response.TransmitFile(Server.MapPath("~/download/test.doc"));
        Response.End();
    }
    protected void LinkButtonDownloadPdf_Click(object sender, EventArgs e)
    {
        Response.ContentType = "Application/pdf";
        Response.AppendHeader("Content-Disposition", "attachment; filename=pf.pdf");
        Response.TransmitFile(Server.MapPath("~/download/pf.pdf"));
        Response.End();

    }
}




0 comments:

Post a Comment