如何从多个输入文件中获取所有URL的图像?

时间:2019-01-17 21:01:41

标签: javascript jquery html

我正在尝试从多个输入文件中获取所有url值。它仅适用于一个图像,但是如果我尝试附加多个图像,则会检索到空响应,并且只能正确获取最后一个图像。希望任何人都可以帮助解决这个问题! 在下面和JSFiddle示例中发布我的代码:

<input type="file" id="files" name="files[]" multiple />
<output id="list"></output>

<script>
  function handleFileSelect(evt) {
    var files = evt.target.files; // FileList object

    // Loop through the FileList and render image files as thumbnails.
    for (var i = 0, f; f = files[i]; i++) {

      // Only process image files.
      if (!f.type.match('image.*')) {
        continue;
      }

      var reader = new FileReader();

      // Closure to capture the file information.
      reader.onload = (function(theFile) {
        return function(e) {
            //show url image
          var dataURL = reader.result;
          alert(dataURL);
        };
      })(f);

      // Read in the image file as a data URL.
      reader.readAsDataURL(f);
    }
  }

  document.getElementById('files').addEventListener('change', handleFileSelect, false);
</script>

https://jsfiddle.net/3q49moyj/

0 个答案:

没有答案