c# - HttpPostedFileBase in Asp.Net MVC 4 with Jasny Bootstrap with existing image saved to S3 -
i need know if image has been removed in jasny bootstrap. saving , removing code works great. problem knowing when image isn't set bc file upload image translated httppostedfilebase when 1 exists.
class:
[notmapped] public httppostedfilebase image { get; set; } edit view:
@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> } save code:
if (data.image == null) { s3utility.delete(myobject.id); } else { s3utility.upload(data.image, myobject.id); } all of works fine. loads image if there 1 (@imageurl, i'm setting).
the problem 'image' httppostedfilebase null on update.
is there way can know if image has been removed or set? , have value updated on setting/removing image in jasny?
i saw post
http://www.jasny.net/articles/jasny-bootstrap-file-upload-with-existing-file/
but didn't translate asp.net mvc 4 , httppostedfilebase.
ok, answer simple. in object added property
[notmapped] public bool existingimage { get; set; } in edit view added hidden field existingimage only if image exist
edit view:
@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"> // new code checking existing image @if (existingimage) { <input type="hidden" name="existingimage" value="true" /> } // end new code </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> } and in save check see if existing image or not:
if (!data.existingimage) { if (data.image == null) { s3utility.delete(myobject.id); } else { s3utility.upload(data.image, myobject.id); } } my problem not understanding how .fileinput-exists section works. when remove or update image, removes in section. if remove or update get's called in jasny, existingimage default false. @ point check see if image null (removed or nonexistent) or if exists (been updated). if image doesn't exist, call delete s3, isn't big deal (for purposes).
Comments
Post a Comment