I have the following code:
[HttpPost]
public ActionResult FileUploadMultiple(IEnumerable<HttpPostedFileBase> files)
{
foreach (var file in files)
{
if (file != null)
{
var fileName = Path.GetFileName(file.FileName);
string extension = Path.GetExtension(file.FileName);
var path = Path.Combine("C://Reports//36000", fileName);
file.SaveAs(path);
}
}
return RedirectToAction("Index");
}
I have 2 files that I am testing on – as such it should loop twice. The first saves fine to the given path. When the code goes and tries to do the second file, I get the following message:
The process cannot access the file ‘C:\Reports\36000\Report #36028.pdf’ because it is being used by another process.
I think I need to do a dispose but, when I tried to do file.Dispose() this didn’t appear to be correct in Intellisense.
You can try this to delete an existing File, before saving: