johnjs
johnjs

Reputation: 25

I'm trying to download PDF file using C# and asp.net

using the following code I'm trying to download pdf file from server path but after clicking on download button file not download but the code gets file name and file path.

I think I'm missing something in code so, please help me out Thank you in advance..

code is : protected void btn_Download_Template_Click(object sender, EventArgs e)

{
     string mainPath = "";
     string fileName = "Self_Declaration.pdf";

     mainPath = Server.MapPath("~/Template/Self_Declaration.pdf");
     
     
     FileInfo file = new FileInfo(mainPath);
     if (file.Exists)
     {
         Response.Clear();
         Response.ClearHeaders();
         Response.ClearContent();
         Response.AddHeader("content-disposition", "attachment; filename= ~/Template/Self_Declaration.pdf");
         Response.AddHeader("Content-Type", "application/pdf");
         Response.ContentType = "application/pdf";
         Response.WriteFile(file.FullName);
         Response.End();
}
else
     {
         Response.Write("This file does not exist.");
     }
}```

Upvotes: 1

Views: 1714

Answers (1)

ANAND PRAKASH TIWARI
ANAND PRAKASH TIWARI

Reputation: 21

Well I don't have enough reputation points to put this in the comment section, but I think your question has already been answered here

Upvotes: 0

Related Questions