Download files from Folder (Directory) on GridView HyperLink Click in ASP.Net using C# and VB.Net

Last Reply 6 months ago By arunkurmi

Posted 6 months ago

How can I open & download pdf from a server folder in which it is saved by click on hyperlink of a gridview using c#

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        if (Request.QueryString["id"] != null)
        {
            int File_link = int.Parse(Request.QueryString["id"].ToString());
            GetDocuments(File_link);
        }
    }
}

protected void GetDocuments(int File_link)
{
    string[] filePaths = Directory.GetFiles(Server.MapPath("~/PDF_File" + File_link));
    List<ListItem> files = new List<ListItem>();
    foreach (string filePath in filePaths)
    {
        files.Add(new ListItem(Path.GetFileName(filePath), filePath));
    }
    Grid_File.DataSource = files;
    Grid_File.DataBind();
}

Viewpdfpage:-

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head2" runat="server">
    <title></title>
</head>
<body>
    <form id="form2" runat="server">
    <div>
    </div>
    </form>
</body>
</html>

 

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        string filePath = Server.MapPath("~/PDF_File" + File_link);
        Response.ContentType = "Application/pdf";
        Response.WriteFile(filePath);
        Response.End();
    }
}

I am getting the error :

Access to the path 'F:\wip\wip\PDF_File' is denied

yet the path is right.

Response.WriteFile (filepath) is giving error.

kindly help in this.

Posted 6 months ago
vrindavani says:
string[] filePaths = Directory.GetFiles(Server.MapPath("~/PDF_File" + File_link));

Change above with below code.

string[] filePaths = Directory.GetFiles(Server.MapPath("~/PDF_File/" + File_link));

 

vrindavani says:
string filePath = Server.MapPath("~/PDF_File" + File_link);

 Change above with below code.

string filePath = Server.MapPath("~/PDF_File/" + File_link);

 


Posted 6 months ago

Hi vrindavani,

Refer below sample and modifiy in your code.

HTML

<asp:GridView runat="server" ID="Grid_File" AutoGenerateColumns="false">
    <Columns>
        <asp:TemplateField HeaderText="S No.">
            <ItemTemplate>
                <%# Container.DataItemIndex + 1 %>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="Text" HeaderText="File" />
        <asp:TemplateField HeaderText="view">
            <ItemTemplate>
                <asp:HyperLink NavigateUrl='<%#string.Format("~/Default.aspx?id={0}",Eval("Text").ToString())%>'
                    runat="server" Text="View" ID="hplView" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Namespace

C#

using System.Collections.Generic;
using System.IO;

VB.Net

Imports System.Collections.Generic
Imports System.IO

Code

C#

Page.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        GetDocuments();
    }
}

protected void GetDocuments()
{
    string[] filePaths = Directory.GetFiles(Server.MapPath("~/PDF_File"));
    List<ListItem> files = new List<ListItem>();
    foreach (string filePath in filePaths)
    {
        files.Add(new ListItem(Path.GetFileName(filePath), filePath));
    }
    Grid_File.DataSource = files;
    Grid_File.DataBind();
}

Default.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        if (!string.IsNullOrEmpty(Request.QueryString["id"]))
        {
            string File_link = Request.QueryString["id"].ToString();
            string filePath = Server.MapPath("~/PDF_File/" + File_link);
            Response.ContentType = "Application/pdf";
            Response.WriteFile(filePath);
            Response.End();
        }
    }
}

VB.Net

Page.aspx.vb

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not IsPostBack Then
        GetDocuments()
    End If
End Sub

Protected Sub GetDocuments()
    Dim filePaths As String() = Directory.GetFiles(Server.MapPath("~/PDF_File"))
    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
    Grid_File.DataSource = files
    Grid_File.DataBind()
End Sub

Default.aspx.vb

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        If Not String.IsNullOrEmpty(Request.QueryString("id")) Then
            Dim File_link As String = Request.QueryString("id").ToString()
            Dim filePath As String = Server.MapPath("~/PDF_File/" & File_link)
            Response.ContentType = "Application/pdf"
            Response.WriteFile(filePath)
            Response.End()
        End If
    End If
End Sub