I want to delete file just after this is downloaded.
I am using .Net Core MVC 6 C# Controlller.
public IActionResult DownloadPDFReport(string fileName)
{
string appBaseUrl = _webHostEnvironment.WebRootPath;
// concatenating FileName + FileExtension
var fileNameToSave = String.Concat(fileName, ".pdf");
// Combines two strings into a path.
var filepath = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "pdf_files")).Root + $@"\{fileNameToSave}";
var verification = _context.Cases.Where(x => x.Id == fileName).FirstOrDefault();
if (verification == null)
{
return NotFound();
}
var doc = _context.EAddress_Documents.Where(x => x.CaseID == fileName).FirstOrDefault();
var reportModel = new ReportViewModel
{
CaseID = fileName,
ReferenceId = verification.Id,
ApplicantName = "Gopal Sharma",
Address = verification.Address,
ClientName = "EXL Services",
PhoneNumber = "9876543210",
Latitude = verification.Latitude,
Longitude = verification.Longitude,
NearbyLocationImageURL = doc.NearbyLocationImage_FilePath
};
PdfService.GenerateReport(filepath, reportModel);
string newFileName = verification.ApplicantName + "_" + DateTime.Now + ".pdf";
string filePath = "~/pdf_files/" + fileName + ".pdf";
Response.Headers.Add("Content-Disposition", "inline; filename=" + newFileName + "");
return File(filePath, "application/pdf");
}
Any help on this ?
I also tried to make an attribue like below. But this is Old MVC Code. This code does not work in .NET Core MVC 6 C#.
public class DeleteFileAttribute : ActionFilterAttribute
{
public override void OnResultExecuted(ResultExecutedContext filterContext)
{
filterContext.HttpContext.Response.Flush();
var filePathResult = filterContext.Result as FilePathResult;
if (filePathResult != null)
{
System.IO.File.Delete(filePathResult.FileName);
}
}
}
Second One--