locked
Enumerating the Images folder in JS RRS feed

  • Question

  • I'm trying to determine the best way in HTML5 / JS to enumerate the contents of the images folder in JavaScript. Think of it like this: I have the \images folder, and a subfolder called \random. I'd like to be able to look into \images\random\ and see the names of the (say) 10 images located in this folder.

    1) Is there a way to do this easily?

    2) Is there a better way of doing this?

    Thursday, November 29, 2012 9:46 PM

Answers

  • Hi,

    Please refer to the code as follow:

    function getImage() {
            // get all the files in the picturesLibrary
            Windows.Storage.KnownFolders.picturesLibrary.getFilesAsync().then(function (files) {
                // get only one of the files, just to show it can be done
                var file = files.getAt(1);
                // set the src attribute to the URL you create from the file object.
                theImage.src = URL.createObjectURL(file);
    
            });
            
    
        }


    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contact msdnmg@microsoft.com
    Microsoft One Code Framework

    • Marked as answer by Song Tian Sunday, December 2, 2012 4:07 AM
    Friday, November 30, 2012 3:32 AM
  • Song,

    This is for the known pictures folder, located in the user's workspace. I asked for the \images folder (actually, the \images\random).

    I have discovered the answer. For the images folder, this function should work:

                Windows.ApplicationModel.Package.current.installedLocation.getFolderAsync("images").done(function (folder) {
    
                    folder.getFilesAsync().then(function (files) {
    
                        files.forEach(function (file) {
    
                            var tmp = file;
    
                        });
    
                    });
    
    
                });
    Hope this helps someone else.

     

     

     

     

     


    • Marked as answer by FearTheDonut Friday, November 30, 2012 1:47 PM
    • Edited by FearTheDonut Friday, November 30, 2012 1:47 PM
    Friday, November 30, 2012 1:47 PM

All replies

  • Hi,

    Please refer to the code as follow:

    function getImage() {
            // get all the files in the picturesLibrary
            Windows.Storage.KnownFolders.picturesLibrary.getFilesAsync().then(function (files) {
                // get only one of the files, just to show it can be done
                var file = files.getAt(1);
                // set the src attribute to the URL you create from the file object.
                theImage.src = URL.createObjectURL(file);
    
            });
            
    
        }


    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contact msdnmg@microsoft.com
    Microsoft One Code Framework

    • Marked as answer by Song Tian Sunday, December 2, 2012 4:07 AM
    Friday, November 30, 2012 3:32 AM
  • Song,

    This is for the known pictures folder, located in the user's workspace. I asked for the \images folder (actually, the \images\random).

    I have discovered the answer. For the images folder, this function should work:

                Windows.ApplicationModel.Package.current.installedLocation.getFolderAsync("images").done(function (folder) {
    
                    folder.getFilesAsync().then(function (files) {
    
                        files.forEach(function (file) {
    
                            var tmp = file;
    
                        });
    
                    });
    
    
                });
    Hope this helps someone else.

     

     

     

     

     


    • Marked as answer by FearTheDonut Friday, November 30, 2012 1:47 PM
    • Edited by FearTheDonut Friday, November 30, 2012 1:47 PM
    Friday, November 30, 2012 1:47 PM