Get file size

Hi every body,

Today i’m trying to get the size of a file which is located in my app folder. I tried to use

    window.resolveLocalFileSystemURL(myFileCompleteURL, function(fileEntry) {
        fileEntry.file(function(fileObj) {
          this.consoleLog += "fileObj.size : " + fileObj.size + "\r\n";
        },
        function(err){
          this.consoleLog += "err : " + err + "\r\n";
        });
    })

But it doesn’t work…No error, no result…I didn’t find anything else to get file size with ionic / cordova…Anyone got an idea ?

thanks :wink:

no error, no result : perhaps not called ?
did you put debugger; in it ?
what is there in the fileObj object?

The function seems to be not called…

import { Component } from '@angular/core';
import { NavController, NavParams } from 'ionic-angular';
import { Camera } from '@ionic-native/camera';
declare var window;

@Component({
  selector: 'page-photo',
  templateUrl: 'photo.html',
})
export class PhotoPage {

  constructor(public navCtrl: NavController, 
      public navParams: NavParams,
      private camera: Camera) {  }

  onTakePhoto(){
    this.camera.getPicture({
      quality: 100,
      targetWidth: 900,
      targetHeight: 600,
      destinationType: this.camera.DestinationType.FILE_URI,
      encodingType: this.camera.EncodingType.JPEG,
      mediaType: this.camera.MediaType.PICTURE,
      saveToPhotoAlbum: false,
      sourceType: this.camera.PictureSourceType.CAMERA
    }).then((imagedata) => {

      window.resolveLocalFileSystemURL(imagedata, function(fileEntry) {
        fileEntry.file(function(fileObj) {
         console.log("fileObj.size : " + fileObj.size);
        },
        function(err){
         console.log(err);
        });
    })

    })

  }

}

This is how I get the file size,

this.file.resolveLocalFilesystemUrl(fileUri).then(fileEntry => {
    fileEntry.getMetadata((metadata) => {
        console.log(metadata.size);//metadata.size is the size in bytes
        ....
    }
    ....
}
3 Likes

Thanks, it works !!!

This is my code… can you look into it…

But the code is not working in android… but it complies fine.

My code is here…
this.camera.getPicture(options).then((imageData) => {
let loading = this.loadingCtrl.create();
loading.present();

  this.file.resolveLocalFilesystemUrl(imageData).then((oneFile: any) => {
     this.file.readAsArrayBuffer(this.file.tempDirectory, oneFile.name).then(realFile => {
       let type = 'jpg';
       let newName = this.awsProvider.randomString(6) + new Date().getTime() + '.' + type;

       this.awsProvider.getSignedUploadRequest(newName, type).subscribe(data => {
         let reqUrl = data.signedRequest;

         this.awsProvider.uploadFile(reqUrl, realFile).subscribe(result => {
           this.awsProvider.getSignedFileRequest(newName).subscribe(res => {
             this.images.push({Key:name, url: res});
             loading.dismiss();
         });
       });
     });
  });      
});

});

Thanks in advance

Here is how to do that

this.file.resolveLocalFilesystemUrl(uri).then(fileEntry=> {
fileEntry.getMetadata(function(metaData) {
//console.log(JSON.stringify(metaData));
return ((metaData.size/1024)/1024); //SIZE IN MB
},
err =>{

        });
    },
    err =>{
        
    });

Just to complement your answer:

return this.file.resolveLocalFilesystemUrl(fileURI).then(fileEntry => {
	fileEntry.getMetadata((metadata) => {
		let fileSize = metadata.size/1024/1024 ;
		if (fileSize > 30) {
			// If size is bigger than 30MB, return
			console.log('file too big');
			return;
		}  
		console.log("metadata size in MB: ", metadata.size/1024/1024);		        
	});
});