Hi,
you can set file name in command argument of link button.
Try the following sample to understand.
HTML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" EmptyDataText="No files uploaded">
<Columns>
<asp:BoundField DataField="Text" HeaderText="File Name" />
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkDownload" Text="View File" CommandArgument='<%# Eval("Text") %>' runat="server" OnClick="ViewFile"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code
C#
Default
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string[] filePaths = Directory.GetFiles(Server.MapPath("~/Reports/"));
List<ListItem> files = new List<ListItem>();
foreach (string filePath in filePaths)
{
files.Add(new ListItem(Path.GetFileName(filePath), filePath));
}
GridView1.DataSource = files;
GridView1.DataBind();
}
}
protected void ViewFile(object sender, EventArgs e)
{
string url = string.Format("Home.aspx?FN={0}", (sender as LinkButton).CommandArgument);
string script = "<script type='text/javascript'>window.open('" + url + "')</script>";
this.ClientScript.RegisterStartupScript(this.GetType(), "script", script);
}
Home
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string filePath = Server.MapPath("~/Reports/") + Request.QueryString["FN"];
this.Response.ContentType = "application/pdf";
this.Response.AppendHeader("Content-Disposition;", "attachment;filename=" + Request.QueryString["FN"]);
this.Response.WriteFile(filePath);
this.Response.End();
}
}
VB.Net
Default
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim filePaths As String() = Directory.GetFiles(Server.MapPath("~/Reports/"))
Dim files As List(Of ListItem) = New List(Of ListItem)()
For Each filePath As String In filePaths
files.Add(New ListItem(Path.GetFileName(filePath), filePath))
Next
GridView1.DataSource = files
GridView1.DataBind()
End If
End Sub
Protected Sub ViewFile(ByVal sender As Object, ByVal e As EventArgs)
Dim url As String = String.Format("Home.aspx?FN={0}", (TryCast(sender, LinkButton)).CommandArgument)
Dim script As String = "<script type='text/javascript'>window.open('" & url & "')</script>"
Me.ClientScript.RegisterStartupScript(Me.GetType(), "script", script)
End Sub
Home
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim filePath As String = Server.MapPath("~/Reports/") + Request.QueryString("FN")
Me.Response.ContentType = "application/pdf"
Me.Response.AppendHeader("Content-Disposition;", "attachment;filename=" & Request.QueryString("FN"))
Me.Response.WriteFile(filePath)
Me.Response.[End]()
End If
End Sub
Screenshot
This is just sample for your problem.
1.First fetch desired file name.and send it to the next page by querystring.
2.Construct the file path as i did in sample .
3.then write file in response stream to display.
4.This will provide functionality to display pdf on new window
use same sample and download file using datakey instead of CommandArgument