It will be useful if there is some functionality, which allows checking the File size.
Workaround:
TypeScript
import { EventData } from 'data/observable';
import { Page } from 'ui/page';
import { HelloWorldModel } from './main-view-model';
import { knownFolders, File } from "file-system";
import { isAndroid } from "platform"
declare var java: any
declare var NSFileManager: any;
declare var NSFileSize: any;
export function navigatingTo(args: EventData) {
let page = <Page>args.object;
page.bindingContext = new HelloWorldModel();
}
export function onTap() {
var documents = knownFolders.currentApp();
var imgfile: File = <File>documents.getFile('logo.png');
console.log("path");
console.log(imgfile.path);
if (isAndroid) {
console.log("Android");
var file = new java.io.File(imgfile.path);
var length = file.length();
var lengthB = length / 1024;
console.log("file size in bytes")
console.log(lengthB);
}
else {
console.log("iOS");
var defManager = NSFileManager.defaultManager;
var fileAttributes = defManager.attributesOfItemAtPathError(imgfile.path);
console.log(defManager);
var fileSizeNumber = fileAttributes.objectForKey(NSFileSize);
console.log(fileSizeNumber);
var fileSizeNumberB = fileSizeNumber / 1000;
console.log("file size in bytes")
console.log(fileSizeNumberB)
}
}
Does it make sense to wrap-up this workaround in to a basic plugin?
it works well. Thanks.
My case is to integrate it with nativescript-imagepicker. After selected image, the 'selected.fileUri' will be "file:///the-file-path.jpg". Trim the 'file:', the left is the imgfile.path for defManager.attributesOfItemAtPathError(imgfile.path);
PR pending: #5710 will add @tsonevn 's code to the file system module.
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Most helpful comment
PR pending: #5710 will add @tsonevn 's code to the file system module.