this is the sample code by which i am downloading pdf at client pc
$("#btndownload").click(function () {
window.location = '@Url.Action("GeneratePDF", "TestModal")';
return false;
});
public void GeneratePDF()
{
bool IsPdfGenerated = false;
List<Student> studentsVM = new List<Student>
{
new Student {ID=1,FirstName="Joy", LastName="Roy", FavouriteGames="Hocky"},
new Student {ID=2,FirstName="Raja", LastName="Basu", FavouriteGames="Cricket"},
new Student {ID=3,FirstName="Arijit", LastName="Banerjee",FavouriteGames="Foot Ball"},
new Student {ID=4,FirstName="Dibyendu", LastName="Saha", FavouriteGames="Tennis"},
new Student {ID=5,FirstName="Sanjeeb", LastName="Das", FavouriteGames="Hocky"},
};
var viewToString = StringUtilities.RenderViewToString(ControllerContext, "~/Views/Shared/_Report.cshtml", studentsVM, true);
string filepath = HttpContext.Server.MapPath("~/PDFArchives/") + "mypdf.pdf";
Document pdfDoc = null;
try
{
StringReader sr = new StringReader(viewToString);
pdfDoc = new Document(PageSize.A4, 10f, 10f, 30f, 0f);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, new FileStream(filepath, FileMode.Create));
pdfDoc.Open();
XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr);
pdfDoc.Close();
IsPdfGenerated = true;
}
catch (Exception ex)
{
IsPdfGenerated = false;
}
System.Web.HttpContext.Current.Response.ContentType = "pdf/application";
System.Web.HttpContext.Current.Response.AddHeader("content-disposition", "attachment;" +
"filename=sample101.pdf");
System.Web.HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
System.Web.HttpContext.Current.Response.Write(pdfDoc);
System.Web.HttpContext.Current.Response.End();
}
i know the pdf file path stored in filepath . after download begin how could i delete the pdf file at server side ? help me with sample code. thanks