【问题标题】:Get pictures from a given directory using JavaScript使用 JavaScript 从给定目录获取图片
【发布时间】:2012-11-01 04:05:37
【问题描述】:

我正在使用 JavaScript 开发 Windows 8 应用程序。我需要获取给定目录中存在的所有图片(每张图片的路径)。我该怎么做?

我有以下几点:

(function () {
    "use strict";
    var page = WinJS.UI.Pages.define("/html/scenario3.html", {
        ready: function (element, options) {
            document.getElementById("folder").addEventListener("click", pickFolder, false);
        }
    });

    function pickFolder() {
        // Clean scenario output
        WinJS.log && WinJS.log("", "sample", "status");

        // Verify that we are currently not snapped, or that we can 
        // unsnap to open the picker
        var currentState = Windows.UI.ViewManagement.ApplicationView.value;
        if (currentState === Windows.UI.ViewManagement.ApplicationViewState.snapped && !Windows.UI.ViewManagement.ApplicationView.tryUnsnap()) {
            // Fail silently if we can't unsnap
            return;
        }

        // Create the picker object and set options
        var folderPicker = new Windows.Storage.Pickers.FolderPicker;
        folderPicker.suggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.desktop;
        // Users expect to have a filtered view of their folders depending on the scenario.
        // For example, when choosing a documents folder, restrict the filetypes to documents for your application.
        folderPicker.fileTypeFilter.replaceAll(["*"]);
        //folderPicker.fileTypeFilter.replaceAll([".png", ".jpg", ".jpeg"]);

        folderPicker.pickSingleFolderAsync().then(function (folder) {
            if (folder) {
                // Application now has read/write access to all contents in the picked folder (including sub-folder contents)
                // Cache folder so the contents can be accessed at a later time
                Windows.Storage.AccessCache.StorageApplicationPermissions.futureAccessList.addOrReplace("PickedFolderToken", folder);

                var pictures = Windows.Storage.KnownFolders.folder;
                pictures.getFilesAsync().done(function (images) {
                    console.log(images.length + " images found.");
                    WinJS.log && WinJS.log("Total Images: " + images.length, "sample", "status");
                });


            } else {
                // The picker was dismissed with no selected file
                WinJS.log && WinJS.log("Operation cancelled.", "sample", "status");
            }

        });
    }
})();

如果我使用上面的代码,它会给出以下错误:

Unable to get property 'getFilesAsync' of undefined or null reference

【问题讨论】:

  • 欢迎使用 stackoverflow。你试过什么?
  • 感谢 daniel,其实这是我第一次使用 javascript 开发 win 8 应用程序,在 c# 中我知道文件可以通过 Directory.GetFiles 获取,
  • 我知道如何选择目录,但不知道如何过滤给定目录中存在的所有图片
  • 除非我遗漏了 Windows 8 的一些重要方面,否则我认为 JavaScript 无法访问文件系统。
  • 我正在尝试,现在我可以访问给定的目录,我想我非常接近解决方案。

标签: javascript microsoft-metro winjs


【解决方案1】:

如果您需要从图片库加载图片,您首先需要在项目的package.appxmanifest 文件中的“功能”选项卡中检查该功能是否已启用。

启用后,您可以通过以下方式引用此库:

// 'pictures' is a reference to our Pictures Library
var pictures = Windows.Storage.KnownFolders.picturesLibrary;

Microsoft 决定任何超过 50 毫秒的操作都需要是异步的,因此要从该库中获取图像,我们必须使用以下异步方法,并在 promise 解决时处理结果:

// Get a list of files ('images') within our Pictures Library
pictures.getFilesAsync().done(function( images ) {
    console.log( images.length + " images found." );
});

您可以从这里遍历找到的图像并随意处理它们。

有关详细信息,请查看 Window.Storage 命名空间。

此外,您可能会发现File access sample 也很有帮助。

更新

从您更新的代码中,我可以看到您允许用户选择他们选择的文件夹。有了这个,您需要设置 fileTypeFiltering,然后在 pickSingleFolderAsync 承诺解决后拉取图像:

// Prompt user to select a folder
var picker = Windows.Storage.Pickers.FolderPicker();

// Which file types will we be showing?
picker.fileTypeFilter.append(".jpg");

// Grab the selected folder, reference as 'folder'
picker.pickSingleFolderAsync().then(function (folder) {
    // Get files within selected folder as 'files'
    folder.getFilesAsync().then(function (files) {
        // Output number of files found
        console.log(files.length + " files found.");
    });
});

【讨论】:

  • 它给我一个错误:JavaScript 运行时错误:无法获取未定义或空引用的属性“getFilesAsync”
  • @mshahbazm 仔细检查您的代码。如果您愿意,也可以在此处发布更新。
  • folder 不是knownFolders 的成员。另外,你在哪里声明folderPicker
  • 当我使用图片库时,你的代码效果很好,但是当我用用户选择的文件夹替换图片库时,就会发生错误
  • 如果选中的文件夹不是成员函数,那么如何访问其内容
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-12-13
  • 1970-01-01
  • 2018-07-15
  • 2013-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多