How to work with temporary files in ASP.NET?

Note that I am not talking about the compiler-created "ASP.NET Temporary Files".

My web application (ASP.NET MVC) uses Graphviz to generate images that are then sent to the client. This requires the creation of temporary files.

What is the best way to handle this? Is there any way to delete them immediately after they are sent? Should I use a background thread? Something in Application_Start or Application_End?

+5
source share
6 answers

You could not do this through the controller or use ASHX ( http://www.marklio.com/marklio/CommentView,guid,df8d6471-83fd-4f66-a799-ef8274979f0e.aspx ) to release the content and delete the temporary files after that How did you finish recording the stream?

+3
source

Graphviz creates the client and adds it as a link to the page. therefore, you cannot delete them directly.

There are several ways:

  • when starting the application, no one should use one of these images. so you can delete it
  • you add a link to the image (like a path) to the cache and add a CacheItemRemovedCallback that will delete your image. (significantly reduces the number of images on your HD
  • make a timer that periodically removes items.

, , . - .

+3

, , , Last-Modifed , , If-Modified-Since StatusCode 304 "NOT Modified" , , .

+2

, , . : ( MVC, )

public ActionResult Foo()
{
    FooCleanup(); // deletes files in "~/temp/Foo/" older than a day or so

    string filename = CreateTemporaryFile(); // Creates a temporary file like "~/temp/Foo/{timestamp}.foo"
    return File(filename);
}

Foo() , , . , .

+1

application_start , 24 / .

0

(.ashx) . , , , .

, , . ( ...)

Although it would be best if you could avoid the problem with the temp file throughout , and stream on demand, generating it in a handler ...

0
source

All Articles