C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
ASP.NET Download FileASP.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. ASP.NET 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.csusing 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"; } } } Output: This application will prompt a window to download the file from the server.
Next TopicASP.NET Cookie
|