51

I want to delete a file immediately after download, how do I do it? I've tried to subclass FilePathResult and override the WriteFile method where I delete file after

HttpResponseBase.TransmitFile

is called, but this hangs the application.

Can I safely delete a file after user downloads it?

George Stocker
  • 57,289
  • 29
  • 176
  • 237
Valentin V
  • 24,971
  • 33
  • 103
  • 152

13 Answers13

89

You can return just regular FileStreamResult which is opened with FileOptions.DeleteOnClose. File stream will be disposed with result by asp.net. This answer dosen't require usage of low level response methods which may backfire in certain situations. Also no extra work will be done like loading file to memory as whole.

var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.None, 4096, FileOptions.DeleteOnClose);
return File(
    fileStream: fs,
    contentType: System.Net.Mime.MediaTypeNames.Application.Octet,
    fileDownloadName: "File.abc");

This answer is based on answer by Alan West and comment by Thariq Nugrohotomo.

Risord
  • 2,116
  • 1
  • 15
  • 13
  • This worked perfectly for me. Simplest solution by far, in my opinion. – Denis M. Kitchen Nov 10 '17 at 16:01
  • 2
    I highly recommend this answer above the others. (And remember to upvote Thariq Nugrohotomo's comment on this answer as well: https://stackoverflow.com/a/14763098/170309 ) – sfarbota Jun 13 '18 at 19:47
  • This answer worked best for me as I cannot load the file into memory due to it's size and no extra action attributes or delegates required. – mr.coffee Jun 06 '19 at 02:23
  • Should definitely be the accepted answer. – Jason Jul 09 '21 at 10:45
  • This should be the accepted answer. Method worked perfectly for me. Love it's elegance! – Tim Jun 21 '22 at 16:36
37

Read in the bytes of the file, delete it, call the base controller's File action.

public class MyBaseController : Controller
{
    protected FileContentResult TemporaryFile(string fileName, string contentType, string fileDownloadName)
    {
        var bytes = System.IO.File.ReadAllBytes(fileName);
        System.IO.File.Delete(fileName);
        return File(bytes, contentType, fileDownloadName);
    }
}

BTW, you may refrain from this method if you're dealing with very large files, and you're concerned about the memory consumption.

Alan West
  • 762
  • 1
  • 7
  • 8
  • 1
    are you sure this is a valid answer for this question? – Oerd Feb 07 '13 at 23:47
  • 1
    +1 I like this one better then the accepted answer. There is no attribute or request flushing required, and the code is obvious: Read the filebytes into memory, delete the phisical file and return the bytes from memory to the user. Thanks! – Céryl Wiltink Jan 22 '14 at 12:25
  • 7
    This solution does not scale, for large files or lots of users. The memory consumption will grow exponentially as users and/or file size grows. – harsimranb Jan 21 '15 at 19:47
  • 33
    for better scalability, you can switch into FileStreamResult, and the stream itself should be opened with `FileOptions.DeleteOnClose`. This way after you pass the stream and ASP done with the stream, the stream will be closed and file automatically deleted. (except on system crash duh) – Thariq Nugrohotomo May 05 '15 at 08:13
35

Create file and save it.

Response.Flush() sends all data to client.

Then you can delete temporary file.

This works for me:

FileInfo newFile = new FileInfo(Server.MapPath(tmpFile));

//create file, and save it
//...

string attachment = string.Format("attachment; filename={0}", fileName);
Response.Clear();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = fileType;
Response.WriteFile(newFile.FullName);
Response.Flush();
newFile.Delete();
Response.End();
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
biesiad
  • 2,258
  • 4
  • 19
  • 16
23

Above answers helped me, this is what I ended up with:

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);
     }
  }
}
Trax72
  • 958
  • 5
  • 12
14

You could create a custom actionfilter for the action with an OnActionExecuted Method that would then remove the file after the action was completed, something like

public class DeleteFileAttribute : ActionFilterAttribute 
{ 
    public override void OnActionExecuted(ActionExecutedContext filterContext) 
    { 
        // Delete file 
    } 
} 

then your action has

[DeleteFileAttribute]
public FileContentResult GetFile(int id)
{
   ...
}
Ian
  • 33,605
  • 26
  • 118
  • 198
Israfel
  • 1,162
  • 2
  • 15
  • 29
  • The download hangs and never finishes, ideas? – Valentin V Jan 11 '10 at 12:32
  • Of course, I wasnt thinking, you're deleting the file before the download is complete. Could you not delete the previously generated file before each request for a new result, thats what I've done in a similar sounding situation. – Israfel Jan 11 '10 at 12:34
  • It adds unnecessary complexity, I should store downloaded files in DB then since there is no way to keep the previos file known to subsequent. – Valentin V Jan 11 '10 at 12:38
  • This idea is workable, however it doesn't work unless the response is flushed as mentioned in Trax72's answer below. – NightWatchman Jul 10 '18 at 16:04
  • 1
    For me: onActionExecuteddid did not work correctly - but 'OnResultExecuted' worked (see alternative answers below). – Thomas Byrne Apr 03 '19 at 10:03
6

I performed same action in WebAPI. I needed to delete file just after it downloaded form server. We can create custom response message class. It takes file path as parameter and delete it once its transmitted.

 public class FileHttpResponseMessage : HttpResponseMessage
    {
        private readonly string filePath;

        public FileHttpResponseMessage(string filePath)
        {
            this.filePath = filePath;
        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
            File.Delete(filePath);
        }
    }

Use this class as below code and it will delete your file once it will be written on response stream.

var response = new FileHttpResponseMessage(filePath);
            response.StatusCode = HttpStatusCode.OK;
            response.Content = new StreamContent(new FileStream(filePath, FileMode.Open, FileAccess.Read));
            response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
            {
                FileName = "MyReport.pdf"
            };
            return response;
Rahul Garg
  • 4,069
  • 1
  • 34
  • 31
4

overriding the OnResultExecuted method is probably the correct solution.. This method runs after the response is written.

public class DeleteFileAttribute : ActionFilterAttribute 
{ 
    public override void OnResultExecuted(ResultExecutedContext filterContext) 
    { 
        filterContext.HttpContext.Response.Flush();
        // Delete file 
    } 
} 

Action code:

[DeleteFileAttribute]
public FileContentResult GetFile(int id)
{
   //your action code
}
Baz1nga
  • 15,485
  • 3
  • 35
  • 61
3

Here is updated answer based on elegant solution by @biesiad for ASP.NET MVC ( https://stackoverflow.com/a/4488411/1726296)

Basically it returns EmptyResult after response is sent.

public ActionResult GetFile()
{
    string theFilename = "<Full path your file name>"; //Your actual file name
        Response.Clear();
        Response.AddHeader("content-disposition", "attachment; filename=<file name to be shown as download>"); //optional if you want forced download
        Response.ContentType = "application/octet-stream"; //Appropriate content type based of file type
        Response.WriteFile(theFilename); //Write file to response
        Response.Flush(); //Flush contents
        Response.End(); //Complete the response
        System.IO.File.Delete(theFilename); //Delete your local file

        return new EmptyResult(); //return empty action result
}
Community
  • 1
  • 1
Tejasvi Hegde
  • 2,694
  • 28
  • 20
  • i used this solution, but i need to delete file before response.end, because after is not reachable – elle0087 Oct 14 '19 at 13:00
2

Try This. This will work properly.

public class DeleteFileAttribute : ActionFilterAttribute
{
  public override void OnResultExecuted( ResultExecutedContext filterContext )
  {
    filterContext.HttpContext.Response.Flush();
    string filePath = ( filterContext.Result as FilePathResult ).FileName;
    File.Delete( filePath );
  }
}
Hrushikesh Patel
  • 266
  • 4
  • 12
1

My used pattern.

1)Create file.

2)Delete old created file, FileInfo.CreationTime < DateTime.Now.AddHour(-1)

3)User downloaded.

How about this idea?

takepara
  • 10,413
  • 3
  • 34
  • 31
1

SOLUTION:

One should either subclass the FileResult or create a custom action filter, but the tricky part is to flush the response before trying to delete the file.

Valentin V
  • 24,971
  • 33
  • 103
  • 152
1

I preferred a solution which returned an HttpResponseMessage. I liked the simplicity of Risord's answer, so I created a stream in the same manner. Then, instead of returning a File, I set the HttpResponseMessage.Content property to a StreamContent object.

var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.None, 4096, FileOptions.DeleteOnClose);
return new HttpResponseMessage()
{
    Content = new StreamContent(fs)
};
JavaJudt
  • 787
  • 7
  • 8
-1

I have posted this solution in https://stackoverflow.com/a/43561635/1726296

public ActionResult GetFile()
    {
            string theFilename = "<Full path your file name>"; //Your actual file name
            Response.Clear();
            Response.AddHeader("content-disposition", "attachment; filename=<file name to be shown as download>"); //optional if you want forced download
            Response.ContentType = "application/octet-stream"; //Appropriate content type based of file type
            Response.WriteFile(theFilename); //Write file to response
            Response.Flush(); //Flush contents
            Response.End(); //Complete the response
            System.IO.File.Delete(theFilename); //Delete your local file

            return new EmptyResult(); //return empty action result
    }
Community
  • 1
  • 1
Tejasvi Hegde
  • 2,694
  • 28
  • 20