The controller issues a file when requested.

public ActionResult Index() { return File(FilePath, MediaTypeNames.Text.Plain, "File.txt"); } 

I need to, as soon as the file returns (it’s all that will be given, not a break in the network occurs) - delete it, but since the ActionResult returns a value after return, I can’t perform another method after that.
How to run the method as soon as the ActionResult worked?

  • Not strong in MVC, but the first thing that comes to mind is to return a copy of the result. Those. declare "ActionResult temp;" inside the method, then get the contents of the file in temp, delete the file and return temp. - Aquinary
  • You can transfer the download to the byte array, delete the file, and transfer the byte array to stackoverflow.com/questions/3604562/… - Serginio
  • You can also try using try finaly - Serginio
  • Please correct the title of the question. You have a question about files, not about return. - Pavel Mayorov

2 answers 2

Based on https://stackoverflow.com/questions/3604562/download-file-of-any-type-in-asp-net-mvc-using-fileresult

 public FileResult Download() { byte[] fileBytes = System.IO.File.ReadAllBytes(@"c:\folder\myfile.ext"); string fileName = "myfile.ext"; System.IO.File.Delete(@"c:\folder\myfile.ext"); return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName); } 

You can also use temporary files like

How do I automatically delete tempfiles in c #?

 using (FileStream fs = new FileStream(Path.GetTempFileName(), FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None, 4096, FileOptions.RandomAccess | FileOptions.DeleteOnClose)) { // temp file exists } 

You can also see the API for temp file here.

  • @ "Serginio" principle yes, but theoretically there is a chance that when downloading a large file we will load it into memory and delete the file itself, and then we will give it from the memory ..... and then the Internet will disappear .... and the file is gone That to repeat the procedure is embarrassing in principle. - Rakzin Roman
  • Then tempo files that will be deleted when closing using using - Serginio
  • Try catch finaly - Serginio
  • That is, in catch to restore the file - Serginio
  • Another option is to report the delivery of the file from the client and delete it on the server - Serginio

To make sure that the recipient received the file completely, just wait for the ACK packet to arrive at the last byte sent. But as far as I know, existing TCP drivers do not allow this information to be obtained.

In addition, the HTTP.SYS driver and the IIS web server hide the real TCP socket.

Therefore, the output could be to place a separate samopny HTTP server on a separate port that would not use persistent connections — in this case, the file can be deleted after the end of the incoming stream.


But there is another problem. The fact is that any proxy server will be happy to cache the result of the request - and forget to give it to the client. And browsers are happy to make fun of the protocol (for example, forget to close the connection) ...

Therefore, you need to decide what is more important: delete the file or deliver it to the user.

In the second case, it is best to delete the file by timer. For example, a month after the first download.

In the first case, you can take this code as a basis: FilePathResult.cs - and add the file deletion there after the full upload.