Check Size of a File from Ionic (Solved)

Hello everyone,

I was wondering if someone here could guide me in the right direction, I want to create a file picker, most likely using the http://ionicframework.com/docs/v2/native/file/ plugin.

Is there a way to check the file size before doing anything with it?

I’d like to upload those files to a server, but want to check the size to only allow uploading to sizes < 5MB for example.

Where should I be looking for info about this?

EDIT: Ok it looks like I need to read more about handling files with JS, that information is already in the Blob file.

Cheers!
Jorge

Hi,
Did you get any solution to get the size of a file, I wanted same functionality for my application, if you can provide some code for the solution it would be helpful.

Like I said on the edit, it’s one of the properties of the object once you get it from the file picker :slight_smile:

Could you show how please? getFile(directoryEntry, fileName, flags) does not contain the file size.

I want to upload file of 25 Mb or less in size to remote server. Anybody can help how to check file size in ionic 2??

Here is a working service example that list an image folder on the device
and retrieve image file informations such as : name, url, size, date, resolution.
if it can help…

If a cleaner way exists, please let us know

import { Injectable } from '@angular/core';
import { File } from '@ionic-native/file';

declare var window;
declare var cordova;

@Injectable()
export class FileService {

    constructor(private file: File) {}

    public listAppImages() {

        this.file.listDir(cordova.file.dataDirectory, "myImagesFolder").then((files) => {
            files.forEach((file, index) => {

                console.log("image name : " + file.name);
                console.log("image url : " + file.nativeURL);

                window.resolveLocalFileSystemURL(file.nativeURL, (fileEntry) => {

                    fileEntry.getMetadata((metadata) => {
                        console.log("image size : " + metadata.size);
                        console.log("image date : " + metadata.modificationTime);
                    });

                    fileEntry.file(function(fileObject){
                        var reader = new FileReader()
                        reader.onloadend = function(evt) {
                            var image = new Image()
                            image.onload = function(evt) {
                        	console.log("image resolution: " + this["width"] + " x " + this["height"]);
                                image = null
                            }
                            image.src = evt.target["result"]
                        }
                        reader.readAsDataURL(fileObject)

                    }, function(){ console.error("There was an error reading or processing this file.") })
                }, (error) => { console.error(error); });
            });
        }).catch((error) => { console.error(error) });
    }

}
1 Like

how can I use this in my template to show the list of each file name, size and modification date.

by reading this Check Size of a File from Ionic (Solved)

hey did you find the solution?
if yes then please share.
Thank you

window.resolveLocalFileSystemURL this is not firing. do you have any idea what’s wrong in that ?
Please help.

Are you running it from the mobile device (android/ios), cause from ionic serve it will not work

Hello,
Its not return size and modificationTime;
How can get them? in order to set to view