locked
Display pdf file in asp net gridview from database physical path RRS feed

  • Question

  • User-1190924364 posted

    I have uploaded PDF file in specifc location Eg: "E:\PDFUpload\" and the path has is stored in database.

    Now i would like to display the gridview with PDF file where user can view and download the file.

    Kindly help me, i have searched mostly there are storing the files with in application.

    Saturday, October 10, 2020 7:16 AM

Answers

  • User-1330468790 posted

    Hi umerfaiz007@outlook.com,

     

    If you are looking into a way to view or download pdf files via a click on button inside the grid view control, you could refer to below codes.

    I didn't use a real database. Instead, I used a simulation of database to store the paths for pdf files.

     

    The settings for this demo are:

    • Two files are put in folder "E:\TestResource", 123.pdf and 321 pdf. 
    • Each button is associated with Id as the identifier for files.  
    • Each button could trigger the event of the grid view control "OnRowCommand".
    • The difference between "View" and "Download" is the response header.

     

    ASPX Page:

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowCommand="GridView1_RowCommand">
                    <Columns>
                        <asp:BoundField DataField="Id" HeaderText="ID" />
                        <asp:BoundField DataField="Name" HeaderText="File Name" />
                        <asp:TemplateField HeaderText="View PDF" >
                            <ItemTemplate>
                                <asp:Button ID="ViewBtn" runat="server" CommandArgument='<%#Eval("Id") %>' CommandName="View" Text="View"></asp:Button>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="View PDF" >
                            <ItemTemplate>
                                <asp:Button ID="DownloadBtn" runat="server" CommandArgument='<%#Eval("Id") %>' CommandName="Download"  Text="Download"></asp:Button>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>

    Code behind:

    // Simulation of the database
            private static DataTable _database;
            public static DataTable Database
            {
                get
                {
                    if (_database is null)
                    {
                        _database = new DataTable();
    
                        _database.Columns.Add("Id", typeof(int));
                        _database.Columns.Add("Name", typeof(string));
    
                        _database.Rows.Add(1, "123.pdf");
                        _database.Rows.Add(2, "321.pdf");
    
                    }
    
                    return _database;
                }
                set
                {
                    _database = value;
                }
            }
    
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    BindGridView();
                }
            }
    
            protected void BindGridView()
            {
                GridView1.DataSource = Database;
                GridView1.DataBind();
            }
    
            protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
            {
                int fileId = Convert.ToInt32(e.CommandArgument);
    
                string folderPath = "D:\\TestResource\\";
    
                string fileName = (from row in Database.AsEnumerable()
                                   where row.Field<int>("Id") == fileId
                                   select row.Field<string>("Name")).ToArray().FirstOrDefault();
    
                if (fileName != null)
                {
                    string pdfPath = folderPath + fileName;
    
                    StreamReader streamReader = new StreamReader(pdfPath);
    
                    Stream stream = streamReader.BaseStream;
    
                    BinaryReader binaryReader = new BinaryReader(stream);
    
                    byte[] sendbyteArray = binaryReader.ReadBytes(Convert.ToInt32(binaryReader.BaseStream.Length));
    
                   
    
                    if (e.CommandName.Equals("View"))
                    {
    
                        Response.ContentType = "application/pdf";
                        Response.AddHeader("Content-Type", "application/pdf");
                        Response.AddHeader("Content-Disposition", "inline");
                        Response.BinaryWrite(sendbyteArray);
                        Response.End();
    
    
                    }
    
                    if (e.CommandName.Equals("Download"))
                    {
                        Response.ContentType = "application/pdf";
                        Response.AppendHeader("Content-Disposition", "attachment; filename=MyFile.pdf");
                        
                        Response.BinaryWrite(sendbyteArray);
                        Response.End();
                    }
    
                }
    
    
                
            }

    Demo:

     

    Hope helps.

    Best regards,

    Sean

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 12, 2020 8:37 AM