jquery - Upload images with preview -
i have panel image uploading. can upload , multiple of images. when choose image displays in other panel (image preview). problem work 1 image not more one. here code. form uploading.
@using (html.beginform("upload", "imageadvertisement", formmethod.post, new { enctype = "multipart/form-data" })){ @html.antiforgerytoken(); <h4><label><b><i>@goonwork.resource.chooseimage</i></b></label></h4> <input type="file" multiple="multiple" class="jfilestyle" name="file" id="file" style="width: 100%;margin: 0 auto;" data-buttontext="><span class='glyphicon glyphicon-folder-open'></span> choose file"; /> <input type="hidden" value="@model" name="advertisementid" /> <br> <br> <button type="submit" class="btn btn-default" style="width:250px;"><span class="glyphicon glyphicon-upload"></span> @goonwork.resource.upload</button> }
and panel display image before upload it.
<div class="col-md-7 hidden" id="showimg"> <div class="panel panel-default"> <div class="panel-heading text-center"><b><i>@goonwork.resource.imagepreview</i></b></div> <div class="panel-body text-center"> <img id="myimg" src="#" alt="your image" class="img-rounded" width="400" height="360" border="0" /> </div> </div> </div>
here jquery part of code.
<script type="text/javascript"> $(function () { $(":file").change(function () { if (this.files && this.files[0]) { var reader = new filereader(); reader.onload = imageisloaded; reader.readasdataurl(this.files[0]); } }); }); function imageisloaded(e) { $('#myimg').attr('src', e.target.result); $('#showimg').removeclass("hidden"); }; </script>
you can preview multiple images following. iterate through each image of file upload, create <img>
, append in dom.
$('input[type="file"]').change(function() { $('.preview').html(''); $.each(this.files, function() { readurl(this); }) }); function readurl(file) { var reader = new filereader(); reader.onload = function(e) { $('.preview').append('<img src=' + e.target.result + ' style="width: 100px; height: 120px;"/>'); } reader.readasdataurl(file); }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="file" multiple/> <div class="preview"></div>
Comments
Post a Comment