Nativescript: [Feature request] FileSystem module - Ability to check File size

Created on 24 Mar 2017  路  4Comments  路  Source: NativeScript/NativeScript

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)

    }

}
help wanted

Most helpful comment

PR pending: #5710 will add @tsonevn 's code to the file system module.

All 4 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

OscarLopezArnaiz picture OscarLopezArnaiz  路  3Comments

rLoka picture rLoka  路  3Comments

Pourya8366 picture Pourya8366  路  3Comments

vtisnado picture vtisnado  路  3Comments

pocesar picture pocesar  路  3Comments