0

Hi all i have vb code below which i am tring to call when someone click on the link.

  Private Sub DownloadFile(ByVal fname As String, ByVal forcedownload As Boolean)
    Dim flpth As String
    Dim fnm As String
    Dim ext As String
    Dim tp As String

    flpth = System.IO.Path.GetFullPath(Server.MapPath(fname))
    fnm = System.IO.Path.GetFileName(flpth)
    ext = System.IO.Path.GetExtension(fnm)
    tp = ""

    If Not IsDBNull(ext) Then
        ext = LCase(ext)
    End If

    Select Case ext
        Case ".doc", ".rtf"
            tp = "application/msword"
        Case ".pdf"
            tp = "application/pdf"
        Case ".zip"
            tp = "application/zip"
    End Select

    If (forcedownload) Then
        Response.Clear()
        Response.ClearContent()
        Response.ClearHeaders()
        Response.ContentType = tp
        Response.AppendHeader("Content-Disposition", "attachment; filename=" + fnm + ext)
        Response.TransmitFile(flpth)
        Response.Flush()
        Response.End()
    End If

End Sub

I am tring to call above code in below line but its not working. Please can any body help.

<a id="Click" runat="server" href="#"  onclick="DownloadFile('files/Notes.doc',True)">Click here</a>
kami
  • 259
  • 4
  • 13

5 Answers5

2

The correct way is to add a link button by drag and drop it to the page, and then go to the properties of this button and add the OnClick methodo, that is also create an automate function on code behind, where you run the DownloadFile

Aristos
  • 66,005
  • 16
  • 114
  • 150
1

1) Try using a LinkButton.

2) You can't pass arguments back, you'll have to get the data at the server.

Steve Wellens
  • 20,506
  • 2
  • 28
  • 69
1

If you are flexible with the fact that it has to be an anchor tag directly, and don't mind creating it as a 'button', you could use the following:

ASP.Net Button in codebehind that calls codebehind function

If not, here is a solution that uses JavaScript and a postback to achieve a similar functionality. Personally I think using the above button solution would be more flexible and close to what you're wanting.

Can I call a code behind function from a div onclick event?

Community
  • 1
  • 1
Rick Petersen
  • 734
  • 5
  • 15
0

If you use the anchor tag as a server control you would need to set the onServerClick event. You can add custom tags to your anchor in order to be used.

<a id="hypDownload" href="javascript:void(0);" runat="server" onserverclick="DownloadFile()" filename="files/Notes.doc" forcedownload="true"></a>

Private Sub hypDownload_ServerClick(sender As Object, e As EventArgs) Handles hypDownload.ServerClick
    Dim filename As String = hypDownload.Attributes("filename") 'Also:  CType(sender, HtmlAnchor).Attributes("filename")
    Dim forcedownload As Boolean = hypDownload.Attributes("forcedownload").ToString().ToLower() = "true"
    DownloadFile(filename, forcedownload)
End Sub
Clarice Bouwer
  • 3,631
  • 3
  • 32
  • 55
0

place this javascript on the ASPX page.

<script type="text/javascript">   
    function DownloadFile() {    
        document.getElementById(<%= DownloadFile.ClientID%>).click();
    }    
</script>

place the button within a div tag with display style hidden DO NOT set the display style of the button to hidden as the javascript will not be able to find the button on the page.

<div style="display= hidden;">
    <asp:button id="DownloadFile" runat="server" />    
</div>

then setup you <a> tag as shown below:

<a href="javascript:DownloadFile();">link text</a>

then use your sub routine as the click event for the asp:button

having said all that, the asp:linkbutton option would result in a lot less code.