我正试图从Win 8应用中的图片中获取大小(像素,咬合)。我用openPicker选择文件然后得到文件。但我找不到尺寸属性? 如果文件很大,我会显示错误。 知道如何获得这些信息吗? 感谢。
答案 0 :(得分:0)
经过几个小时的搜索和尝试,我已经解决了这个问题:
//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....
});
});
});
希望它可以帮助其他人。
答案 1 :(得分:0)
您可以通过StorageFile对象本身获取此信息,而无需打开文件或将其加载到内存中。
为方便起见,假设myFile是有问题的StorageFile。
对于图像尺寸,请调用myFile.properties.getImagePropertiesAsync,其结果是包含width和height属性的ImageProperties对象。您可以在Simple imaging sample方案1中找到检索ImageProperties的示例。
对于文件大小,请致电myFile.getBasicPropertiesAsync。结果是BasicProperties对象,其size属性具有该值。