Limit uploaded image size Camera plug-in cordova ionic

I uploaded the image using the Camera plugin in my ionic application. I want to limit the user to the size of the image that the user selects, say, 200 kb. I added the "Camera as file" plugin.

I used the code below:

/*Function to get image from gallery*/
$scope.getImageFromGallery = function(){
    var options = {
        quality: 100,
        destinationType: Camera.DestinationType.FILE_URI,
        sourceType: Camera.PictureSourceType.SAVEDPHOTOALBUM,
        popoverOptions: CameraPopoverOptions,
        saveToPhotoAlbum: false,
        targetWidth: 450,
        targetHeight: 450,
        encodingType: Camera.EncodingType.JPEG,
    };

    navigator.camera.getPicture(gallerySuccess, galleryError, options);

    function gallerySuccess(imageURI){
        getSize(imageURI);          
    }


    function getSize(fileUri) {
        window.resolveLocalFileSystemURL(fileUri, function(fileEntry){

            fileEntry.getMetadata(function(metadata){
                console.log("size is "+metadata.size);
            }, resOnError);

            fileEntry.file(function(file) {

                var reader = new FileReader();
                reader.onloadend = function(evt) {

                    var imgData = evt.target.result;
                    var res = imgData.split(",");
                    $rootScope.imagebase64 = res[1];
                    var image = document.getElementById('preview-image1');
                    image.src = evt.target.result;
                    $('#preview-image').css('display','block');
                    $('#preview-image').css('background-image', "url("+res[1]+")").show();
                };

                reader.readAsDataURL(file);
            }, resOnError);
        },
        resOnError);
    }

    function resOnError(error) {
        console.log("error "+JSON.stringify(error));
    }

    function galleryError(error) {
    }
}

But now, when I select a 2.5 MB image, it shows a size of 178908 and when I select an image of 8.9 MB, it shows a size of 88412.

As far as I know, the file size is in bytes, but the values ​​I get are incorrect.

+4
source share
1 answer

try this solution, this is for 150kb for yours, just change it to 200kb

  window.resolveLocalFileSystemURI(newPath, function (fileEntry) {
                        fileEntry.file(function (fileObj) {
                            if (fileObj.size <= 153600) {
                                var reader = new FileReader();
                                reader.onload = function () {
                                    var dataURL = reader.result;
                                    //your stuff.....
                                };
                                reader.readAsDataURL(fileObj);
                            }
                            else {
                                //  alert("Please upload image less then 150KB");
                                alertPopup = $ionicPopup.alert({
                                    title: 'Upload image failed!',
                                    template: 'Please upload image less then 150KB!'
                                });
                            }

                            console.log("Size = " + fileObj.size);
                        });
                    });
0

Source: https://habr.com/ru/post/1685152/


All Articles