Windows Universal文件选择器允许视频

时间:2017-08-18 14:35:31

标签: windows win-universal-app

我目前在我的应用中输入了一个文件:

<input type="file" (change)="fileChange($event)" placeholder="Select from gallery" accept=".png,.jpg,.jpeg"/>

这应该只允许选择.png .jpg.jpeg个文件,但它实际上允许在Windows Universal Platform上运行时选择所有文件。 (Windows 10 Mobile)。

有没有办法可以强制Windows通用平台只允许选择图像?或者我应该在用户选择文件后抓住这个?

1 个答案:

答案 0 :(得分:0)

  

有没有办法可以强制Windows通用平台只允许选择图像?或者我应该在用户选择文件后抓住这个?

您可以使用Windows.Storage.Picers.FileOpenPicker来完成该操作,而不是使用Html的FilePicker:

HTML:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>TestFilePicker</title>
    <link href="css/default.css" rel="stylesheet" />
</head>
<body>
    <div>Content goes here!</div>
    <button id="btnClick">Click Me to Pick File</button>
    <script src="js/main.js"></script>
</body>
</html>

main.js:

(function () {
"use strict"

    document.getElementById("btnClick").onclick = function (evt)
    {
        var openPicker = new Windows.Storage.Pickers.FileOpenPicker();
        openPicker.viewMode = Windows.Storage.Pickers.PickerViewMode.thumbnail;
        openPicker.suggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.picturesLibrary;
        openPicker.fileTypeFilter.replaceAll([".png", ".jpg", ".jpeg"]);

        // Open the picker for the user to pick a file
        openPicker.pickSingleFileAsync().then(function (file) {
            if (file) {
                // Application now has read/write access to the picked file
            } else {
                // The picker was dismissed with no selected file
            }
        });
    }
})();