Clearing MultipartFormDataStreamProvider

If files are sent to my webapp, I read them through MultipartFormDataStreamProvider.FileData .

Initialize the provider as follows:

 string root = HttpContext.Current.Server.MapPath("~/App_Data"); var provider = new MultipartFormDataStreamProvider(root); 

And the provider stores them perfectly as "~ App_Data / BodyPart_ {someguid}" But how do I clear these files after I finish with them?

+7
source share
2 answers

You can delete all files that are older than a certain period of time. eg.

 private void CleanTempFiles(string dir, int ageInMinutes) { string[] files = Directory.GetFiles(dir); foreach (string file in files) { var time = File.GetCreationTime(file); if (time.AddMinutes(ageInMinutes) < DateTime.Now) { File.Delete(file); } } } 

Then call it something like:

 CleanTempFiles(root, 60); // Delete all files older than 1 hour 
+1
source share

I know this question is old, but the best way to find a temporary file was after processing it.

 string root = HttpContext.Current.Server.MapPath("~/App_Data"); var provider = new MultipartFormDataStreamProvider(root); foreach (var file in provider.Files) { // process file upload... // delete temporary file System.IO.File.Delete(file.LocalFileName); } 
+1
source share

All Articles