I have a controller class which inherits from ApiController
and handles HTTP requests from a client.
One of the actions generates a file on the server and then sends it to the client.
I'm trying to figure out how to clean up the local file once the response has been fulfilled.
Ideally this would be done through an event which fires once a response has been sent to the client.
Is there such an event? Or is there a standard pattern for what I'm trying to achieve?
[HttpGet]
public HttpResponseMessage GetArchive(Guid id, string outputTypes)
{
//
// Generate the local file
//
var zipPath = GenerateArchive( id, outputTypes );
//
// Send the file to the client using the response
//
var response = new HttpResponseMessage(HttpStatusCode.OK);
var stream = new FileStream(zipPath, FileMode.Open);
response.Content = new StreamContent(stream);
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/zip");
response.Content.Headers.ContentLength = new FileInfo(zipPath).Length;
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = Path.GetFileName(zipPath)
};
return response;
}