javascript - Preview multiple uploaded images in symfony -


i wanna preview images javascript before uploading it, i'm working in symfony & use filetype form.. here code :

{% block content %}      {{ form_start(form, {'attr': {'id': 'image_form', 'class': 'form-horizontal container'}} ) }}      <div class="col-md-6">                 <div class="form-group">                 <div class="col-md-3 text-right">                     {{ form_label(form.name, 'images upload :', {'label_attr': {'class': 'control-label'}}) }}                 </div>                 <div class="col-md-8">                      <div id="wrapper" style="margin-top: 20px;">                     {{ form_widget(form.name, {'attr': {'id' : 'fileupload'}}) }}                     </div>                     {{ form_errors(form.name) }}                 </div>             </div>          <div class="col-md-offset-7">              {{ form_label(form.upload) }}             {{ form_widget(form.upload, { 'label': 'upload', 'attr': {'class': 'btn btn-info'}}) }}             <button type="reset" class="btn btn-default">clear</button>         </div>         <div id="image-holder"></div>     </div>          {{ form_end(form) }}  {% endblock %}      {% block javascripts %}          {{ parent() }}          <script>             $(document).ready(function() {                 $("#fileupload").on('change', function() {                     //get count of selected files                     var countfiles = $(this)[0].files.length;                     var imgpath = $(this)[0].value;                     var extn = imgpath.substring(imgpath.lastindexof('.') + 1).tolowercase();                     var image_holder = $("#image-holder");                     image_holder.empty();                     if (extn == "gif" || extn == "png" || extn == "jpg" || extn == "jpeg") {                         if (typeof(filereader) != "undefined") {                             //loop each file selected uploaded.                             (var = 0; < countfiles; i++)                             {                                 var reader = new filereader();                                 reader.onload = function(e) {                                     $("<img />", {                                         "src": e.target.result,                                         "class": "thumb-image"                                     }).appendto(image_holder);                                 }                                 image_holder.show();                                 reader.readasdataurl($(this)[0].files[i]);                             }                         } else {                             alert("this browser not support filereader.");                         }                     } else {                         alert("pls select images");                     }                 });             });         </script>     {% endblock %} 

i, tried same thing simple html input of type of file, & working fine, why doesn't in symfony form?

it's because symfony2 form_div_layout adding own id every widget

{%- block widget_attributes -%}   id="{{ id }}" name="{{ full_name }}"   .... 

so have change {'id' : 'fileupload'} {'class' : 'fileupload'} , $("#fileupload").on('change' $(".fileupload").on('change' , check result. or rewrite form layout.


Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -