我得到這個錯誤ASP.NET MVC刪除文件?
The process cannot access the file '..\Images\Temp\6574_1212665562989_1419270107_30610848_6661938_n.jpg' because it is being used by another process.
當我嘗試這樣做:
try
{
var file = Request.Files["FileProfilePicture"];
file.SaveAs(Server.MapPath("~/Images/Temp/" + file.FileName));
Bitmap imageOrj = new Bitmap(System.Web.HttpContext.Current.Server.MapPath("~/Images/Temp/" + file.FileName));
Image imageBig = ResizeImage.Resize(imageOrj, 100, 100);
imageBig.Save(System.Web.HttpContext.Current.Server.MapPath("~/Images/ProfilePicBig/" + file.FileName));
Image imageSmall = ResizeImage.Resize(imageOrj, 50, 50);
imageSmall.Save(System.Web.HttpContext.Current.Server.MapPath("~/Images/ProfilePicSmall/" + file.FileName));
string[] files = System.IO.Directory.GetFiles(Server.MapPath("~/Images/Temp/"));
foreach (string pathFile in files)
{
System.IO.File.Delete(pathFile);
}
return RedirectToAction("Index", "Author");
}
catch (Exception e)
{
ModelState.AddModelError("", "Kullanıcı bilgileri güncellenirken bir hata oluştu. Lütfen daha sonra tekrar deneyin." + e.Message);
}
我怎樣才能解決這個問題。或者另一種更好的方式來保持圖像作爲臨時。我應該將文件保存在臨時文件夾中嗎?
謝謝
保存後如何關閉文件。 – 2012-04-24 12:55:29
我不確定FileStream()。Close()會解決它,它不是一個FileStream,而是一個HttpPostedFileBase。但SaveAs方法不應該打開文件,只是從字面上創建一個副本(或者將HttpPostedFile刷新到該文件中),因此它必須是引發抖動的Bitmap對象......這意味着這是保持文件打開的文件,所以我想你可以在完成Bitmap後使用imageOrj.Dispose()... – army 2012-04-24 18:27:11
我認爲Jakub的文章中的鏈接是一個很好的例子,所以如果這能解決你的問題,你可以接受.. – army 2012-04-24 18:29:17