open download pdf from a server folder

Last Reply 10 days ago By arunkurmi

Posted 11 days 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#

You are viewing reply posted by: arunkurmi 10 days ago.
Posted 10 days 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