Saturday, 13 June 2020

ASP.NET Download Button

ASP.NET Download Button Code in Asp.net

ASP.NET provides implicit object Response and its methods to download file from the server. We can use these methods in our application to add a feature of downloading file from the server to the local machine.

Here, we are creating an example that allows us to download file.


// Webform1.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="DownloadButton.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
    <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous"></script>
    <title></title>
</head>
<body style="background-color:#eeeeee;">
    <form id="form1" runat="server" class="form-group" style="background-color:#ffffff; width:500px; height:auto; margin:50px auto 10px auto; padding:30px; border-radius:8px; overflow:hidden;box-shadow:0 2px 10px -3px #333;">
        <div align="center" >
            <p style="text-decoration-style:solid">Download the latest CORONAVIRUS status</p>
            <asp:Button ID="Button1" runat="server"  Text="Download"  Class="form-control;btn btn-success" OnClick="Button1_Click" />
        </div>
        <asp:Label ID="Label1" runat="server"></asp:Label>
    </form>
</body>
</html>


// Webform1.aspx.cs

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

namespace DownloadButton
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            
            try
            {
                Response.ContentType = "Application/octect-stream";
                Response.AppendHeader("Content-Disposition", "attachment; filename=Coronalive.xlsx");
                Response.TransmitFile(Server.MapPath("~/Files/CoronaLive.xlsx"));
                Response.End();
            }
            catch(Exception ex)
            {
                Response.Write(ex.Message);
            }
            
           
            }
    }
}

// Design:-



1 comment: