views:

48

answers:

2

I have a Gridview that shows a list of files previously uploaded to the server with a HyperLink control to be able to download it, I need to force a download every time the user clicks on one of the provided links, so that the file does not open directly on the browser (they are usually images). Currently I have a side-server function that forces a download, but I do not know how to assign this function to each one of the links when the user clicks on it.

A: 

Create a download page that takes the download parameters by GET query

in the Gridview create a template field with hyperlink column to open the download page in external window to download the sent file after that it closes by it self, I provided a sample code

<Columns>
 <asp:TemplateField>
  <ItemTemplate>
   <a href="downloadPage.aspx?filename=<%# Eval("File") %>" target="_blank">Download</a>
  </ItemTemplate>
 </asp:TemplateField>
</Columns>
Kronass
+1  A: 

Thanks guys, I worked it out using an ImageButton and assigning it a RowCommand that calls my ForceDownload method:

HTML:

<asp:TemplateField HeaderText="Download File">
   <ItemTemplate>
      <asp:ImageButton ID="ibDownload" runat="server" CommandName="Download" CommandArgument='<%# Bind("path") %>' />
   </ItemTemplate>
</asp:TemplateField>

Code Behind:

Private Sub gvFileList_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvFileList.RowCommand
    If e.CommandName = "Download" Then
        Dim myPath = Server.MapPath(e.CommandArgument)
        ForceDownload(HttpContext.Current, myPath)
    End If
End Sub
murderdoll