i try to download a pdf file on button click via ajax call using ASP MVC model when i click on my button, nothing happen but when i add the controller methode on the url my file is downloaded. i want to download it only on button click only
JS:
$('#PrintTimeSheet').click(function () {
$.ajax({
type: 'POST',
url: "/Home/DownloadFile",
success: function (response) {
}
});
});
Controller:
public FileResult DownloadFile()
{
Document PDF = new Document();
MemoryStream memoryStream = new MemoryStream();
PdfWriter writer = PdfWriter.GetInstance(PDF, memoryStream);
PDF.Open();
PDF.Add(new Paragraph("Something"));
PDF.Close();
byte[] bytes = memoryStream.ToArray();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment;filename=Receipt-test.pdf");
Response.BinaryWrite(memoryStream.ToArray());
return File(bytes, "application/pdf");
}