How to download file with asp.net on buttton's onClick event?

前端 未结 5 1861
时光取名叫无心
时光取名叫无心 2021-02-06 15:37

I have a String variable (in C#) that contain the full path of PDF file on my server (like that \"~/doc/help.pdf\").

I want that in click on button, this file will down

5条回答
  •  别那么骄傲
    2021-02-06 16:12

    With an ASP button:

    button.OnClientClick = string.Format("javascript:window.location='{0}'", pdfLink);
    

    The logic behind this is here: Client-side click

    This will not reload the page and do a post-back, it will just navigate to the PDF which will be displayed in the browser.


    Set the button's onclick event to this scriptlet:

    onclick="javascript:window.location='/doc/help.pdf'"
    

    To create that server-side:

    
    

    Where PDFLink is a string property in the code behind:

    public string PDFLink
    {
        get
        {
            return "/doc/link.pdf";
        }
    }
    

    From here it should be trivial to take the string from the database and render it absolute if need be.

提交回复
热议问题