Jasny Fileuploadを使用して、画像を記事に追加および更新しています。ユーザーが記事を追加または編集する場合、ユーザーが既存の画像を使用して記事を編集することを選択したものの、画像を変更または削除しない場合を除いて、すべて正常に機能します。
基本的に、この場合、ファイル入力は null であるため、現時点でのコードは、ユーザーが画像を削除したと想定し、それに応じてモデル プロパティを設定します。
私のコントローラーには次のものがあります。
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>
}
コントローラーで、ユーザーが画像を削除または変更するのではなく、画像を変更していないかどうかを確認するにはどうすればよいですか?