简体   繁体   中英

Click Gridview row download particular file in asp.net

In my application, I have a gridview control with 6 columns. When I click on the 6th column row I need to download that file. I am binding gridview from database. How can I take gridview column as like a link?

My markup:

<asp:GridView ID="GridView1" Width="950px" CssClass="Grid" runat="server" AutoGenerateColumns="false" >
    <Columns>
        <asp:BoundField DataField="ID" HeaderText="" ItemStyle-ForeColor="White" />
        <asp:BoundField DataField="Name" HeaderText="Name" />
        <asp:BoundField DataField="SName" HeaderText="SName" />                        
        <asp:BoundField DataField="Date" HeaderText="Date" />
        <asp:BoundField DataField="Size" HeaderText="Size(MB)" />     
        <asp:BoundField DataField="Time" HeaderText="Time" />                             
        <asp:HyperLinkField DataTextField="FileName" DataNavigateUrlFields="Id" HeaderText="File Name" ItemStyle-Width = "150" />
    </Columns>        
</asp:GridView>

My image:

enter image description here

Go through this C# code for downloading the file with the specified name in the specified path. I hope this will help.

    String pathOfFile = Server.MapPath("~/ActualPathOfYourFile/" +  fileNameComingFromDatabse);
    byte[] bts = System.IO.File.ReadAllBytes(pathOfFile);
    Response.Clear();
    Response.ClearHeaders();
    Response.AddHeader("Content-Type", "Application/octet-stream");
    Response.AddHeader("Content-Length", bts.Length.ToString());
    Response.AddHeader("Content-Disposition", "attachment; filename=" + fileNameComingFromDatabse);
    Response.BinaryWrite(bts);
    Response.Flush();
    Response.End();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM