如何在更新后删除物理旧图像
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(Job job, HttpPostedFileBase jobimage)
{
if (ModelState.IsValid)
{
System.IO.File.Delete(Path.Combine(Server.MapPath("~/Uploads"),job.JobImage));
string path = Path.Combine(Server.MapPath("~/Uploads"), jobimage.FileName);
jobimage.SaveAs(path);
job.JobImage = jobimage.FileName;
db.Entry(job).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.CategoryId = new SelectList(db.Categories, "Id", "CategoryName", job.CategoryId);
return View(job);
}
这一行:
System.IO.File.Delete(Path.Combine(使用Server.Mappath( “〜/上传”),job.JobImage));
没有任何帮助,谢谢你
答案 0 :(得分:0)
文件删除采取字符串路径 请查看示例代码
// Delete a file by using File class static method...
if(System.IO.File.Exists(@"C:\Users\Public\DeleteTest\test.txt"))
{
// Use a try block to catch IOExceptions, to
// handle the case of the file already being
// opened by another process.
try
{
System.IO.File.Delete(@"C:\Users\Public\DeleteTest\test.txt");
}
catch (System.IO.IOException e)
{
Console.WriteLine(e.Message);
return;
}
}
答案 1 :(得分:0)
添加以下行:
例如,@ Html.HiddenFor(model => model.JobImage)
在您的视图中:
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.Id)
@Html.HiddenFor(model => model.JobImage)