我需要知道是否在 Jasny Bootstrap 中删除了图像。我的保存和删除代码效果很好。问题是知道何时未设置图像,因为文件上传图像仅在存在时才转换为 HttpPostedFileBase。
班级:
[NotMapped]
public HttpPostedFileBase Image { get; set; }
编辑视图:
@using (Html.BeginForm("Edit", "MyObject", FormMethod.Post, new { enctype = "multipart/form-data" })) {
<div class="field">
<div class="fileinput @imageClass" data-provides="fileinput" data-name="Image">
<div class="fileinput-new thumbnail" style="width: 150px; height: 150px;">
<img src="@MyNamespace.Components.S3Utility.DefaultImageUrl">
</div>
<div class="fileinput-preview fileinput-exists thumbnail" style="max-width: 350px; max-height: 350px;">
<img src="@imageUrl">
</div>
<div>
<span class="btn btn-default btn-file">
<span class="fileinput-new">Select image</span>
<span class="fileinput-exists">Change</span>
<input type="file" name="Image">
</span>
<a href="#" class="btn btn-default fileinput-exists" data-dismiss="fileinput">Remove</a>
</div>
}
保存代码:
if (data.Image == null) {
S3Utility.Delete(myObject.Id);
} else {
S3Utility.Upload(data.Image, myObject.Id);
}
所有这些工作正常。如果有正确的图像(@imageUrl,我正在设置),它甚至会加载图像。
问题是 'Image' HttpPostedFileBase 在更新时始终为空。
有什么方法可以知道图像是否已被删除或设置?在 Jasny 中设置/删除图像时是否更新了该值?
我看到了这个帖子
http://www.jasny.net/articles/jasny-bootstrap-file-upload-with-existing-file/
但它没有转化为 Asp.Net MVC 4 和 HttpPostedFileBase。