ASP NET MVC 5删除文件从服务器 [英] ASP NET MVC 5 Delete File From Server
问题描述
查看code:
@if (File.Exists(Server.MapPath("~/Images/Cakes/" + Html.DisplayFor(modelItem => Model.CakeImage))))
{
@model TastyCakes.Models.Cakes
<form name="deletePhoto" action="/Cakes/DeletePhoto" method="post">
@Html.AntiForgeryToken()
File name of image to delete (without .jpg extension):
<input name="photoFileName" type="text" value="@Html.DisplayFor(modelItem => Model.CakeImage)" />
<input type="submit" value="Delete" class="tiny button">
</form>
} else {
<p>*File Needs to be uploaded</p>
}
控制器code:
Controller Code:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult DeletePhoto(string photoFileName)
{
ViewBag.deleteSuccess = "false";
var photoName = "";
photoName = photoFileName;
var fullPath = Server.MapPath("~/Images/Cakes/" + photoName);
if (File.Exists(fullPath))
{
File.Delete(fullPath);
ViewBag.deleteSuccess = "true";
}
}
凡说,如果(File.Exists)和File.Delete中,code的波浪在它之下线。所以我试图找出我需要什么语法得到THIF文件中删除。
Where it says if (File.Exists) AND File.Delete, the code has squiggly lines underneath it. So I am trying to figure out what syntax I need to get thif file deleted.
下面是在控制我的code的截图:
Here is a screenshot of my code in the controller:
UPPDATE:我已经得到了code的工作和我如何得到它的工作和想法是怎么来的创建在我的博客一个简单的code例子。
<一href=\"http://httpjunkie.com/2014/724/mvc-5-image-upload-delete/\">http://httpjunkie.com/2014/724/mvc-5-image-upload-delete/
UPPDATE: I have got the code working and created a simple code example on my blog on how I got it working and how the idea came about. http://httpjunkie.com/2014/724/mvc-5-image-upload-delete/
推荐答案
使用 Request.MapPath
string fullPath = Request.MapPath("~/Images/Cakes/" + photoName);
if (System.IO.File.Exists(fullPath))
{
System.IO.File.Delete(fullPath);
}
这篇关于ASP NET MVC 5删除文件从服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!