Preview images before upload

后端 未结 8 1776
不思量自难忘°
不思量自难忘° 2020-12-02 09:27

I have a page with four images for the user to select. I want the user to be able to preview each image on the site before upload.

The JavaScript code below works fo

相关标签:
8条回答
  • 2020-12-02 09:55

    function previewImages() {
    
      var preview = document.querySelector('#preview');
      
      if (this.files) {
        [].forEach.call(this.files, readAndPreview);
      }
    
      function readAndPreview(file) {
    
        // Make sure `file.name` matches our extensions criteria
        if (!/\.(jpe?g|png|gif)$/i.test(file.name)) {
          return alert(file.name + " is not an image");
        } // else...
        
        var reader = new FileReader();
        
        reader.addEventListener("load", function() {
          var image = new Image();
          image.height = 100;
          image.title  = file.name;
          image.src    = this.result;
          preview.appendChild(image);
        });
        
        reader.readAsDataURL(file);
        
      }
    
    }
    
    document.querySelector('#file-input').addEventListener("change", previewImages);
    <input id="file-input" type="file" multiple>
    <div id="preview"></div>

    0 讨论(0)
  • 2020-12-02 09:59
    function appendRows(){
        $i++;
        var html='';
        html+='<div id="remove'+$i+'"><input type="file" name="imagefile[]" accept="image/*" onchange="appendloadFile('+$i+')"><img id="outputshow'+$i+'" height="70px" width="90px"><i onclick="deleteRows('+$i+')" class="fas fa-trash-alt"></i></div>';
         $("#appendshow").append(html);
    }
    
    function appendloadFile(i){
        var appendoutput = document.getElementById('outputshow'+i+'');
        appendoutput.src = URL.createObjectURL(event.target.files[0]);
    }
    
    0 讨论(0)
提交回复
热议问题