我正在使用Jasny Fileupload向文章添加和更新图像。当用户添加或编辑文章时一切正常,除非用户选择使用现有图像编辑文章,但随后不更改或删除图像。
基本上我的文件输入在这种情况下是空的,所以我的代码现在假设用户已经删除了图像并相应地设置了模型属性。
在我的控制器中,我有以下内容:
if (file != null)
{
var fileName = Guid.NewGuid().ToString() + Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("/Content/ArticleImages"), fileName);
file.SaveAs(path);
articleToUpdate.ImagePath = fileName;
}
else
{
articleToUpdate.ImagePath = null;
}
我的观点:
@if (Model.article.ImagePath == null)
{
<div class="fileupload fileupload-new" data-provides="fileupload">
<div class="fileupload-preview thumbnail" style="width: 200px; height: 150px; margin-top: 10px">
</div>
<div>
<span class="btn btn-file"><span class="fileupload-new">Select image</span><span
class="fileupload-exists">Change</span><input type="file" name="file" /></span>
<a href="#" class="btn fileupload-exists" data-dismiss="fileupload">Remove</a>
</div>
</div>
}
else
{
<div class="fileupload fileupload-exists" data-provides="fileupload">
<div class="fileupload-preview thumbnail" style="width: 200px; height: 150px; margin-top: 10px">
<img src="/Content/ArticleImages/@Model.article.ImagePath"/>
</div>
<div>
<span class="btn btn-file"><span class="fileupload-new">Select image</span><span
class="fileupload-exists">Change</span><input type="file" name="file" /></span>
<a href="#" class="btn fileupload-exists" data-dismiss="fileupload">Remove</a>
</div>
</div>
}
我如何在我的控制器中检查用户是否没有修改图像,而不是删除或更改它?