问题
Using org.apache.cordova.file plugin I can selected the file and I get the native path of the file. After that I have to restrict the user to select the file according to there file size. But I can't get that file size. My problem is that I can't get the file Size using that plugin. For this I am using this tutorial.
回答1:
To get file size, you need to access it via metadata as follows:
window.resolveLocalFileSystemURL(filePath,
function (fileSystem) {
fileSystem.getFile(fileName, {create: false},
function (fileEntry) {
fileEntry.getMetadata(
function (metadata) {
alert(metadata.size); // get file size
},
function (error) {}
);
},
function (error) {}
);
},
function (error) {}
);
回答2:
This code is a little more compact, as you don't need to call the getMetadata function, size is already a property of fileEntry
function gotPhoto(imageUri) {
window.resolveLocalFileSystemURI(imageUri,
function(fileEntry) {
fileEntry.file(function(fileObj) {
console.log("Size = " + fileObj.size);
},
function (error) {});
}, function (error) {}
);
}
回答3:
The function window.resolveLocalFileSystemURI is deprecated and furthermore you may need to have a specific dedicated transparent function for it.
Therefore you can declare this function.
function getFileSize(fileUri) {
return new Promise(function(resolve, reject) {
window.resolveLocalFileSystemURL(fileUri, function(fileEntry) {
fileEntry.file(function(fileObj) {
resolve(fileObj.size);
},
function(err){
reject(err);
});
},
function(err){
reject(err);
});
});
}
and then merely use it like this
getFileSize("myFileUri").
then(function(fileSize){
console.log(fileSize);
}).
catch(function(err){
console.error(err);
});
来源:https://stackoverflow.com/questions/32858805/get-selected-file-size-using-org-apache-cordova-file