Monday, 15 February 2010

c# - Download a PDF (not opening it in browser) when clicked -



c# - Download a PDF (not opening it in browser) when clicked -

i have href link pdf file when clicked opens in new page in browser. want download instead of opening in new tab.

so how create pdf file link downloadable instead of opening them in browser?

here's code:

<asp:formview id="formview2" runat="server"> <itemtemplate> <asp:loginview id="loginview1" runat="server"> <loggedintemplate> <asp:hyperlink id="hyperlink1" tooltip="open" cssclass="button" runat="server" navigateurl='<%# eval("pdfurl") %>' text="open" target="_blank"></asp:hyperlink> <br /> </loggedintemplate> <anonymoustemplate> <p>you need log in view book.</p> </anonymoustemplate> </asp:loginview> </itemtemplate> </asp:formview>

code behind:

protected void page_load(object sender, eventargs e) { int bookid = convert.toint32(request.querystring.get("bookid")); using (libraryentities entities = new libraryentities()) { var book = (from r in entities.books r.id == bookid select r); formview2.datasource = book; formview2.databind(); } }

just add together attribute download <asp:hyperlink>.

final code be.

<asp:hyperlink id="hyperlink1" tooltip="open" cssclass="button" runat="server" navigateurl='<%# eval("pdfurl") %>' text="open" target="_blank" download </asp:hyperlink>

note: html5 attribute, work html5 compatible browsers. check link see download attribute back upwards on different browsers - http://caniuse.com/#feat=download

c# asp.net pdf

No comments:

Post a Comment