Hi taruni,
Check this example. Now please take its reference and correct your code.
HTML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" EmptyDataText="No files uploaded"
class="table table-hover" OnRowDataBound="OnRowDataBound">
<Columns>
<asp:BoundField DataField="Text" HeaderText="Junior College Results List" />
<asp:BoundField DataField="Text" HeaderText="Uploaded On" />
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkDownload" Text="Download" CommandArgument='<%# Eval("Value") %>'
runat="server" OnClick="DownloadFile"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Namespaces
C#
using System.IO;
VB.Net
Imports System.IO
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string[] filePaths = Directory.GetFiles(Server.MapPath("~/Results_Section/"));
List<ListItem> files = new List<ListItem>();
foreach (string filePath in filePaths)
{
files.Add(new ListItem(Path.GetFileNameWithoutExtension(filePath), filePath));
}
GridView1.DataSource = files;
GridView1.DataBind();
}
}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[1].Text = e.Row.Cells[1].Text.Substring(e.Row.Cells[1].Text.Length - 10, 10);
}
}
protected void DownloadFile(object sender, EventArgs e)
{
string FilePath = (sender as LinkButton).CommandArgument;
string filename = Path.GetFileName(FilePath);
Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename);
// Write the file to the Response
const int bufferLength = 10000;
byte[] buffer = new byte[bufferLength];
int length = 0;
Stream download = null;
try
{
download = new FileStream(Server.MapPath("~/Results_Section/" + filename), FileMode.Open, FileAccess.Read);
do
{
if (Response.IsClientConnected)
{
length = download.Read(buffer, 0, bufferLength);
Response.OutputStream.Write(buffer, 0, length);
buffer = new Byte[bufferLength];
}
else
{
length = -1;
}
}
while (length > 0);
Response.Flush();
Response.End();
}
finally
{
if (download != null)
{
download.Close();
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim filePaths As String() = Directory.GetFiles(Server.MapPath("~/Results_Section/"))
Dim files As List(Of ListItem) = New List(Of ListItem)()
For Each filePath As String In filePaths
files.Add(New ListItem(Path.GetFileNameWithoutExtension(filePath), filePath))
Next
GridView1.DataSource = files
GridView1.DataBind()
End If
End Sub
Protected Sub OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
e.Row.Cells(1).Text = e.Row.Cells(1).Text.Substring(e.Row.Cells(1).Text.Length - 10, 10)
End If
End Sub
Protected Sub DownloadFile(ByVal sender As Object, ByVal e As EventArgs)
Dim FilePath As String = (TryCast(sender, LinkButton)).CommandArgument
Dim filename As String = Path.GetFileName(FilePath)
Response.ContentType = "application/pdf"
Response.AppendHeader("Content-Disposition", "attachment; filename=" & filename)
Const bufferLength As Integer = 10000
Dim buffer As Byte() = New Byte(9999) {}
Dim length As Integer = 0
Dim download As Stream = Nothing
Try
download = New FileStream(Server.MapPath("~/Results_Section/" & filename), FileMode.Open, FileAccess.Read)
Do
If Response.IsClientConnected Then
length = download.Read(buffer, 0, bufferLength)
Response.OutputStream.Write(buffer, 0, length)
buffer = New Byte(9999) {}
Else
length = -1
End If
Loop While length > 0
Response.Flush()
Response.End()
Finally
If download IsNot Nothing Then
download.Close()
End If
End Try
End Sub
Screenshot