Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
134 views
in Technique[技术] by (71.8m points)

html - How to display multiple input file in javascript

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]);
  }
}
question from:https://stackoverflow.com/questions/65902346/how-to-display-multiple-input-file-in-javascript

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
<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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...