当我尝试删除我刚刚上传的文件时,我收到错误“该进程无法访问该文件,因为它正在被另一个进程使用”(这是在文件上传后至少几秒钟,所以我确定它们是写完了)。任何想法为什么会发生这种情况?PS:我生成的缩略图删除没有问题,但原件不知何故被锁定。
var FileExt = Path.GetExtension(photo.File.FileName);
var FilePath = Path.Combine(Server.MapPath("~/App_Data/" + photo.ClientId), photo.PhotoId.ToString()) + FileExt;
photo.File.SaveAs(FilePath);
var ThumbFilePath = Path.Combine(Server.MapPath("~/App_Data/" + photo.ClientId),photo.PhotoId.ToString() + "_thumbnail") + FileExt;
PhotoTools.MakeThumbnail(FilePath, ThumbFilePath, 0.15);
return RedirectToAction("Create");
在 PhotoTools 类中...
public static void MakeThumbnail(string ImgIn, string ImgOut, double Percent)
{
Image img = Image.FromFile(ImgIn);
double Width = img.Width*Percent;
double Height = img.Height*Percent;
MakeThumbnail(ImgIn, ImgOut, (int)Width, (int)Height);
}
删除功能...
public ActionResult DeleteConfirmed(int id)
{
Client client = db.Clients.Find(id);
db.Clients.Remove(client);
db.SaveChanges();
if (Directory.Exists(Server.MapPath("~/App_Data/" + id)))
{
Directory.Delete(Server.MapPath("~/App_Data/") + id,true);
}
return RedirectToAction("Index");
}