asp.net how to download files 02

ASP.NET – How to download files

In this post we will see how we can download files in ASP.NET. We will create a simple ASP.NET webpage to download files stored inside server folder. Users will be able to click on file name and download files directly. For this example we will use ASP.NET LinkText to create file names on the web page and get its text to download corresponding file stored in the folder. So let’s start.

DOWNLOAD SOURCE CODE.

In your Project solution create a folder named “files” and add 4-5 files as below. This files will be used for downloading:

asp.net how to download files 01
ASP.NET How to download Files – Files to download

 

Create a new page in your ASP.NET project with name downloads.aspx and edit it as below:

downloads.aspx:

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Downloading Files in ASP.NET</title>
<link href="bootstrap.min.css" rel="stylesheet" />
<style>
body
{
padding:5px;
}
a
{
margin:5px;
color:#6a25dd;
text-decoration:none;
font-size:20px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2>DOWNLOAD FILES IN ASP.NET</h2>
<hr />
<i>Click on file name to download it.</i>
<br /><br />
<asp:LinkButton runat="server" ID="wallpaper1" OnClick="linkdownload_Click" Text="Wallpaper1.jpg" />
<br />
<asp:LinkButton runat="server" ID="wallpaper2" OnClick="linkdownload_Click" Text="Wallpaper2.jpg" />
<br />
<asp:LinkButton runat="server" ID="wallpaper3" OnClick="linkdownload_Click" Text="Wallpaper3.jpg" />
<br />
<asp:LinkButton runat="server" ID="document1" OnClick="linkdownload_Click" Text="Document1.pdf" />
<br />
<asp:LinkButton runat="server" ID="document2" OnClick="linkdownload_Click" Text="Document2.pdf" />
</div>
</form>
</body>
</html>

And edit the code file as below:

downloads.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 WebApplication1
{
public partial class downloads : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void linkdownload_Click(object sender, EventArgs e)
{
var element = (LinkButton)sender;

String filename = element.Text.ToString();
String filepath = Server.MapPath("Files/" + filename);

Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
Response.Flush();

Response.TransmitFile(filepath);
Response.End();
}
}
}
asp.net how to download files 02
ASP.NET How to download Files – Result

 

DOWNLOAD SOURCE CODE.

Also see:

How to Upload files in ASP.NET MVC?

Angular JS Bind Grid Html Table from SQL in ASP.NET MVC C#.

Creating AngularJS Login form in MVC ASP.NET Forms C#.

How to Upload File ASP.NET MVC with Database.

How to upload files in ASP.NET?

How to download files in ASP.NET?

ASP.NET Gridview CSS design using Bootstrap.



Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.