Download File Example In ASP.NET
code , code for Download File Example In ASP.NET, ASP.NET Developer : Download File Example.
Default.aspx
<%@ Page Title="Home Page" Language="C#" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="FileDownloadExample._Default" %>
<form id="form1" runat="server">
<p>
Click the button to download a file</p>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Download" />
<br />
<br />
<asp:Label ID="Label1" runat="server"></asp:Label>
</form>
Code
Default.aspx.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace FileDownloadExample
{
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string filePath = "C:\\Users\\Admi\\Desktop\\abc.txt";
FileInfo file = new FileInfo(filePath);
if (file.Exists)
{
// Clear Rsponse reference
Response.Clear();
// Add header by specifying file name
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
// Add header for content length
Response.AddHeader("Content-Length", file.Length.ToString());
// Specify content type
Response.ContentType = "text/plain";
// Clearing flush
Response.Flush();
// Transimiting file
Response.TransmitFile(file.FullName);
Response.End();
}
else Label1.Text = "Requested file is not available to download";
}
}
}
code , code for Download File Example In ASP.NET, ASP.NET Developer : Download File Example.
Default.aspx
<%@ Page Title="Home Page" Language="C#" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="FileDownloadExample._Default" %>
<form id="form1" runat="server">
<p>
Click the button to download a file</p>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Download" />
<br />
<br />
<asp:Label ID="Label1" runat="server"></asp:Label>
</form>
Code
Default.aspx.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace FileDownloadExample
{
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string filePath = "C:\\Users\\Admi\\Desktop\\abc.txt";
FileInfo file = new FileInfo(filePath);
if (file.Exists)
{
// Clear Rsponse reference
Response.Clear();
// Add header by specifying file name
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
// Add header for content length
Response.AddHeader("Content-Length", file.Length.ToString());
// Specify content type
Response.ContentType = "text/plain";
// Clearing flush
Response.Flush();
// Transimiting file
Response.TransmitFile(file.FullName);
Response.End();
}
else Label1.Text = "Requested file is not available to download";
}
}
}
Post a Comment