Pan*_*yay 2 jquery asp.net-mvc-3
我通过jquery调用一个动作方法并尝试删除一个文件.但没有任何反应.该文件仍然存在.
以下是jquery Code
$("#pictureRemove").click(function (e) {
$("#pictureImage").html("<img src='../../Content/Images/noDefaultImage_100.gif'/>");
$(this).hide();
$.ajax({
type: 'POST',
url: '@Url.Action("Remove", "Category")',
data: { fileName: $('#pictureTitle').attr('src') },
dataType: 'json'
// User your JSON response.
});
});
Run Code Online (Sandbox Code Playgroud)
以下是操作方法代码
[HttpPost]
public ActionResult Remove(string fileName)
{
string completFileName = Server.MapPath("~/Content/Images/" + fileName);
System.IO.File.Delete(completFileName);
return Json(true);
}
Run Code Online (Sandbox Code Playgroud)
在这一行:
data: { fileName: $('#pictureTitle').attr('src') }
Run Code Online (Sandbox Code Playgroud)
你似乎是从一些图像的src参数传递fileName参数到控制器动作.所以我想你有一些像这样的图像:
<img id="pictureTitle" src="/Content/images/foo.jpg" />
Run Code Online (Sandbox Code Playgroud)
所以你/Content/images/foo.jpg
在你的控制器动作中传递,你试图删除Server.MapPath("~/Content/Images//Content/images/foo.jpg")
哪个被翻译成c:\wwwroot\Content\Images\Content\images\foo.jpg
不太可能存在并抛出异常.
只需在控制器操作中放置一个断点并检查不同的参数.
这就是说,暴露一个带有文件名并删除服务器上文件的控制器操作是应用程序中的一个巨大安全漏洞.