Click here to Skip to main content
15,897,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hi,

I have a GridView containing all the information and data related to a table, the 'content' table.

I would like to know how to download files by pressing the 'Download' button presented in the GridView.

GridView1 RowCommand Code:

VB
Protected Sub GridView1_RowCommand(sender As Object, e As GridViewCommandEventArgs)
      If e.CommandName = "Download" Then

          Dim index As Integer = Convert.ToInt32(e.CommandArgument)
          Dim row As GridViewRow = GridView1.Rows(index)
          Dim contentID As Integer = Convert.ToInt32(GridView1.DataKeys(index).Value)


          Dim strQuery As String = "SELECT content_name, content_type, content_file FROM content where content_id=@id"

          Dim cmd As SqlCommand = New SqlCommand(strQuery)

          cmd.Parameters.AddWithValue("@id", contentID)

          Dim dt As DataTable = GetData(cmd)

          If dt IsNot Nothing Then

              download(dt)

          End If

      End If
  End Sub


GridView1 Code:

XML
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" AllowSorting="True" AutoGenerateColumns="False" CellPadding="4" DataKeyNames="content_id" ForeColor="#333333" GridLines="None">
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        <Columns>
            <asp:CommandField ButtonType="Button" ShowDeleteButton="True" ShowSelectButton="True">
            <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" />
            <ItemStyle BorderStyle="None" HorizontalAlign="Center" VerticalAlign="Middle" />

            </asp:CommandField>
            <asp:BoundField DataField="content_id" HeaderText="#" InsertVisible="False" ReadOnly="True" SortExpression="content_id">
            <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="30px" />
            <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="30px" />
            </asp:BoundField>
            <asp:BoundField DataField="content_name" HeaderText="Name" SortExpression="content_name">
            <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" />
            <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
            </asp:BoundField>
            <asp:BoundField DataField="content_type" HeaderText="Type" SortExpression="content_type">
            <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" />
            </asp:BoundField>
            <asp:ButtonField ButtonType="Button" CommandName="Download" HeaderText="" Text="Download" />
        </Columns>
        <EditRowStyle BackColor="#999999" />
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#E9E7E2" />
        <SortedAscendingHeaderStyle BackColor="#506C8C" />
        <SortedDescendingCellStyle BackColor="#FFFDF8" />
        <SortedDescendingHeaderStyle BackColor="#6F8DAE" />

    </asp:GridView>


SqlDataSource1 Code:

    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnStringDb1 %>" DeleteCommand="DELETE FROM [content] WHERE [content_id] = @content_id" InsertCommand="INSERT INTO [content] ([content_name], [content_type]) VALUES (@content_name, @content_type)" SelectCommand="SELECT [content_name], [content_type], [content_id] FROM [content]" UpdateCommand="UPDATE [content] SET [content_name] = @content_name, [content_type] = @content_type WHERE [content_id] = @content_id">
        <DeleteParameters>
            <asp:Parameter Name="content_id" Type="Int32" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="content_name" Type="String" />
            <asp:Parameter Name="content_type" Type="String" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="content_name" Type="String" />
            <asp:Parameter Name="content_type" Type="String" />
            <asp:Parameter Name="content_id" Type="Int32" />
        </UpdateParameters>
    </asp:SqlDataSource>



Full Code:
http://pastebin.com/W4kdwfPn[^]

Whenever I click the 'Download' button, nothing happens.

Can anyone point me to the right direction?
Posted

1 solution

Hello,

Please have a look at this article File Download in ASP.NET and Tracking the Status of Success/Failure of Download[^] located here on Code Project.

Basically you will have to binary read the file data and binary write to the response stream. To enable file download dialog before writing the file data you will also need to set the ConteneType, ContentLength and Content-Disposition headers.

Regards,
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900