Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Image preview with multiple input file

How can i preview multiple images uploaded from different inputs? The content from textarea is written inside div seeimg on button press, the textarea contains <img src="$img" id="img1"> ,the id's are from 1 to 15 (img1,img2,img3...img15) and i want the images uploaded by user to be seen inside div seeimg


HTML

<form method="post" enctype="multipart/form-data" id="mainform">
Imagine 1:<br>
 <input type="file" name="img1" id="img1"><br>
     <br><br>     
Imagine 2 :<br>
 <input type="file" name="img2" id="img2"><br>
     <br>
 Imagine 3 :<br>
 <input type="file" name="img3" id="img3"><br>
     <br>
 Imagine 4 :<br>
 <input type="file" name="img4" id="img4"><br>
     <br>
 Imagine 5 :<br>
 <input type="file" name="img5" id="img5"><br>
     <br>
 Imagine 6 :<br>
 <input type="file" name="img6" id="img6"><br>
     <br>
 Imagine 7 :<br>
 <input type="file" name="img7" id="img7"><br>
     <br>
 Imagine 8 :<br>
 <input type="file" name="img8" id="img8"><br>
     <br>
 Imagine 9 :<br>
 <input type="file" name="img9" id="img9"><br>
     <br>
 Imagine 10 :<br>
 <input type="file" name="img10" id="img10"><br>
     <br>
 Imagine 11 :<br>
 <input type="file" name="img11" id="img11"><br>
     <br>
 Imagine 12 :<br>
 <input type="file" name="img12" id="img12"><br>
     <br>
 Imagine 13 :<br>
 <input type="file" name="img13" id="img13"><br>
     <br>
 Imagine 14 :<br>
 <input type="file" name="img14" id="img14"><br>
     <br>
 Imagine 15 :<br>
 <textarea id="insert" name="content"></textarea>
 <input type="file" name="img15" id="img15"><br>
 </form>
 <div id="seeimg">
 </div>

So far the scripts it's showing just the image uploaded on first input file <input type="file" name="img1" id="img1"> ,and if i keep changing the image of the first input it's changing the next instead of the first one.
Jquery

   function readURL(input) {
    if (input.files && input.files[0]) {
        var reader = new FileReader();

        reader.onload = function (e) {
            console.log(e.target.result+" , "+i);
            $('#seeimg #img'+i).attr('src', e.target.result);
        }

        reader.readAsDataURL(input.files[0]);
    }
}


$("form#mainform #img"+i).change(function(){
    readURL(this);
    i++;
});

TL;DR the scripts it's working just for the first file input and it keeps changing others img src if i upload another image.

Fiddle: https://jsfiddle.net/eLss3ojx/6/

like image 206
Orlando GTh Avatar asked Nov 01 '22 04:11

Orlando GTh


1 Answers

I tried hard understand the code ,but i could get that you are having multiple file and you want to show the preview to the user as show as the file is uploaded .

The approach is correct,but there are certain basic mistake , you are using the same id for the img and input type file , this will work out, moreover i could couple of more bugs. To help you to get a over , i have modified you code, which will show preview of different images.

HTML

    <form method="post" enctype="multipart/form-data" id="mainform">
    Imagine 1:<br>
     <input type="file" name="img1" id="img1"><br>
     <img id="preview-img1" />
         <br><br>     
    Imagine 2 :<br>
     <input type="file" name="img2" id="img2"><br>
     <img id="preview-img2" />
         <br>
     Imagine 3 :<br>
     <input type="file" name="img3" id="img3"><br>
     <img id="preview-img3" /> 
         <br>
     Imagine 4 :<br>
     <input type="file" name="img4" id="img4"><br>
     <img id="preview-img4" />     
         <br>
     Imagine 5 :<br>
     <input type="file" name="img5" id="img5"><br>
     <img id="preview-img5" />     
         <br>
     Imagine 6 :<br>
     <input type="file" name="img6" id="img6"><br>
     <img id="preview-img6" />     
         <br>
     Imagine 7 :<br>
     <input type="file" name="img7" id="img7"><br>
     <img id="preview-img7" />     
         <br>
     Imagine 8 :<br>
     <input type="file" name="img8" id="img8"><br>
     <img id="preview-img8" />     
         <br>
     Imagine 9 :<br>
     <input type="file" name="img9" id="img9"><br>
     <img id="preview-img9" />     
         <br>
     Imagine 10 :<br>
     <input type="file" name="img10" id="img10"><br>
     <img id="preview-img10" />     
         <br>
     Imagine 11 :<br>
     <input type="file" name="img11" id="img11"><br>
     <img id="preview-img11" />     
         <br>
     Imagine 12 :<br>
     <input type="file" name="img12" id="img12"><br>
     <img id="preview-img12" />     
         <br>
     Imagine 13 :<br>
     <input type="file" name="img13" id="img13"><br>
     <img id="preview-img13" />     
         <br>
     Imagine 14 :<br>
     <input type="file" name="img14" id="img14"><br>
     <img id="preview-img14" />     
         <br>
     Imagine 15 :<br>
     <textarea id="insert" name="content"></textarea>
     <input type="file" name="img15" id="img15"><br>
      <img id="preview-img15" />    
     </form>

Javascript

      function readURL(input) {
        if (input.files && input.files[0]) {
            var reader = new FileReader();

            reader.onload = function (e) {
                imgId = '#preview-'+$(input).attr('id');
                $(imgId).attr('src', e.target.result);
            }

            reader.readAsDataURL(input.files[0]);
        }
      }


      $("form#mainform input[type='file']").change(function(){
        readURL(this);
      });
like image 103
sanjeev Avatar answered Nov 11 '22 05:11

sanjeev