I would like to delete a temporary file after returning it form action. How can i achieve that with ASP.NET Core:
public IActionResult Download(long id)
{
var file = "C:/temp/tempfile.zip";
var fileName = "file.zip;
return this.PhysicalFile(file, "application/zip", fileName);
// I Would like to have File.Delete(file) here !!
}
The file is too big for returning using memory stream.
File() or PhysicalFile() return a FileResult
-derived class that just delegates processing to an executor service. PhysicalFileResult
's ExecuteResultAsync method calls :
var executor = context.HttpContext.RequestServices
.GetRequiredService<IActionResultExecutor<PhysicalFileResult>>();
return executor.ExecuteAsync(context, this);
All other FileResult-based classes work in a similar way.
The PhysicalFileResultExecutor class essentially writes the file's contents to the Response stream.
A quick and dirty solution would be to create your own PhysicalFileResult
-based class that delegates to PhysicalFileResultExecutor but deletes the file once the executor finishes :
public class TempPhysicalFileResult : PhysicalFileResult
{
public TempPhysicalFileResult(string fileName, string contentType)
: base(fileName, contentType) { }
public TempPhysicalFileResult(string fileName, MediaTypeHeaderValue contentType)
: base(fileName, contentType) { }
public override async Task ExecuteResultAsync(ActionContext context)
{
try {
await base.ExecuteResultAsync(context);
}
finally {
File.Delete(FileName);
}
}
}
Instead of calling PhysicalFile()
to create the PhysicalFileResult
you can create and return a TempPhysicalFileResult
, eg :
return new TempPhysicalFileResult(file, "application/zip"){FileDownloadName=fileName};
That's the same thing PhysicalFile() does :
[NonAction]
public virtual PhysicalFileResult PhysicalFile(
string physicalPath,
string contentType,
string fileDownloadName)
=> new PhysicalFileResult(physicalPath, contentType) { FileDownloadName = fileDownloadName };
A more sophisticated solution would be to create a custom executor that took care eg of compression as well as cleaning up files, leaving the action code clean of result formatting code
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With