Get all images from local folder

后端 未结 2 668
走了就别回头了
走了就别回头了 2021-01-02 06:23

I need a way to get all the images from a local folder for a presentation that will also run locally. There will be no attempt for a server to take the images from a local f

相关标签:
2条回答
  • 2021-01-02 07:04

    Thanks to Patrick Hofman's answer, I modified the code and ended up with this :

    $(document).ready(function(){  
    
      function readMultipleFiles(evt) {
        //Retrieve all the files from the FileList object
        var files = evt.target.files; 
    
        if (files) {
            for (var i=0, f; f=files[i]; i++) {
                  var r = new FileReader();
                r.onload = (function(f) {
                    return function(e) {
                        var contents = e.target.result;
                        $('body').append('<h1>' + f.name + '</h1><img src="learn/' + f.name + '"/>');
                    };
                })(f);
    
                r.readAsText(f);
            }   
        } else {
              alert("Failed to load files"); 
        }
      }
    
      document.getElementById('fileinput').addEventListener('change', readMultipleFiles, false);
    
    });
    
    0 讨论(0)
  • 2021-01-02 07:15

    I think your best option is to use the new File API in Javascript. Is has a lot of functions to read files from the file system.

    <input type="file" id="fileinput" multiple />
    <script type="text/javascript">
      function readMultipleFiles(evt) {
        //Retrieve all the files from the FileList object
        var files = evt.target.files; 
    
        if (files) {
            for (var i=0, f; f=files[i]; i++) {
                  var r = new FileReader();
                r.onload = (function(f) {
                    return function(e) {
                        var contents = e.target.result;
                        alert( "Got the file.n" 
                              +"name: " + f.name + "n"
                              +"type: " + f.type + "n"
                              +"size: " + f.size + " bytesn"
                              + "starts with: " + contents.substr(1, contents.indexOf("n"))
                        ); 
                    };
                })(f);
    
                r.readAsText(f);
            }   
        } else {
              alert("Failed to load files"); 
        }
      }
    
      document.getElementById('fileinput').addEventListener('change', readMultipleFiles, false);
    </script>
    

    (code from here)

    You can find a good explanation and helpful code here.

    0 讨论(0)
提交回复
热议问题