Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 download to the client computer.

I created a button and made onClick event in C#. Now, which code should I write to do that?

like image 393
user3167150 Avatar asked Jan 28 '14 21:01

user3167150


People also ask

How do I download from grid view?

Upload the file using file upload control and click on the Upload button. The uploaded file is displayed in the GridView. Next click on the link button to download the files.

How do I download files from Web API?

Users can either left-click a download link to download the file or right-click the link to choose “ Save Link As ” in the context menu and save the file.


2 Answers

I think you are looking for something like this.

private void Button1_click(object sender, System.EventArgs e)
{
    Response.ContentType = "Application/pdf";
    Response.AppendHeader("Content-Disposition", "attachment; filename=help.pdf");
    Response.TransmitFile(Server.MapPath("~/doc/help.pdf"));
    Response.End();
}
like image 122
Krishanu Dey Avatar answered Sep 17 '22 19:09

Krishanu Dey


Try this code on your btn_Click:

Response.Redirect("~/doc/link.pdf");
like image 40
DA Luces Avatar answered Sep 19 '22 19:09

DA Luces