download pdf file at client pc and remove pdf file from server side folder after download begin

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

Answers:

Answer

You can simply use File.Delete() method after using Response.End():

System.Web.HttpContext.Current.Response.End();
System.IO.File.Delete(filepath); // add this line

If you're using FileResult action and reading file from stream, you can delete the file by using FileOptions.DeleteOnClose while creating FileStream instance with specified file options overload:

[HttpGet]
public FileResult GeneratePDF()
{
    // other stuff

    string filepath = HttpContext.Server.MapPath("~/PDFArchives/") + "mypdf.pdf";

    // other stuff

    var stream = new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.None, 4096, FileOptions.DeleteOnClose);
    return File(stream, "application/pdf", "sample101.pdf"); 
}

As an alternative, you can create custom attribute which applies to FileResult action as provided in this reference.

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.