WinJS图片文件大小

winjs picture file size

本文关键字:文件大小 WinJS      更新时间:2023-09-26

我正在尝试从 Win 8 应用程序中的图片中获取大小(像素、咬合)。我用 openPicker 选择文件并得到文件。但是我找不到尺寸属性?如果文件很大,我将显示一个错误。知道我如何获得此信息吗?谢谢。

您可以通过 StorageFile 对象本身获取此信息,而无需打开文件或将其加载到内存中。

为了方便起见,假设myFile是有问题的StorageFile。

对于图像尺寸,请调用 myFile.properties.getImagePropertiesAsync,其结果是包含宽度和高度属性的 ImageProperties 对象。可以在简单映像示例(方案 1)中找到检索图像属性的示例。

对于文件大小,请调用 myFile.getBasicPropertiesAsync。结果是一个 BasicProperties 对象,其大小属性具有该值。

经过几个小时的搜索和尝试,我像这样解决了它:

//add picture
        document.getElementById("btnAddImg").addEventListener('click', function () {
            // Create the picker object and set options
            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"]);
             openPicker.pickSingleFileAsync().then(function (file) {
                file.openAsync(Windows.Storage.FileAccessMode.read).done(function (stream) {
                    var fileType = file.contentType;
                    var blob = MSApp.createBlobFromRandomAccessStream(fileType, stream);
                    var fdata = new FormData();
                    fdata.append('upload_field', blob);
                    //more code....
                });
             });
        });

希望它能帮助其他人。