0

I am stuck with a problem, I am not able to display multiple input files in my form. I want to display multiple input files, how can I do that?

See this: https://i.ibb.co/GdS3GZ3/Capture.png

In there the 2nd box I want to display my other image.

Note: its working on a single image but I want to display multiple images

Please help.

index.html

 <label for="diposite"><b>Photos</b></label>
      <input type='file' onchange="readURL(this);" multiple/>
      <img id="blah" src="https://i.ibb.co/42x6mCS/image.jpg" alt="your image" />

index.js

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

    reader.onload = function (e) {
      $('#blah')
        .attr('src', e.target.result);
    };
    reader.readAsDataURL(input.files[0]);
  }
}
1

2 Answers 2

1
<script>
    function readURL(input) {
        if (!input.files) {
            return;
        }
        const readers = [];
        const bean = document.getElementById('bean');
        for (let i = 0; i < input.files.length; i++) {
            readers.push(new FileReader());
            readers[i].onload = function(e) {
                const img = new Image();
                img.src = e.target.result;
                bean.appendChild(img);
            };
            readers[i].readAsDataURL(input.files[i]);
        }
    }
</script>
<label for="diposite"><b>Photos</b></label>
<input type='file' onchange="readURL(this);" multiple/>
<div id="bean"></div>
Sign up to request clarification or add additional context in comments.

Comments

0

You need to iterate through input.files. For each file, create a new <img> tag and append it to your page.
html

<label for="diposite"><b>Photos</b></label>
<input type='file' onchange="readURL(this);" multiple/>
<div class="images"></div>

javascript

function readURL(input) {
    if (input.files) {
        for(let i=0; i<input.files.length; i++) {
            var reader = new FileReader();

            reader.onload = function (e) {
                let img = $('<img>').attr('src', e.target.result);
                $('.images').append(img)
            };
            reader.readAsDataURL(input.files[i]);
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.