Jquery to check Array before adding TypeID and Filesize If exist add size value ? Dropzone JS

Viewed 22

I have File type and size of files using Dropzone. I am trying to add in array file type and file sizes because I have to limit file size 5MB per file type here is my code,

         init: function () {
                    this.on("addedfile", function (file) {

                        if (file.size < 5 * 1024 * 1024 && file.type.match('pdf.*')) {
                            alert('File added - ' + file.name + ' - Size - ' + file.size);
                            if (typeof Filecount !== 'undefined' && Filecount != '') {
                                Filecount = parseFloat(Filecount + file.size);
                            }
                                else {
                                Filecount = parseFloat(file.size);
                            }


                            var storage = localStorage.getItem('itemList') || '{"items":[]}';
                            var itemObject = JSON.parse(storage);
                            itemObject["items"].push({ "FileTypeID": $('#LKP_DocType').find(":selected").val(), "FileTotalSize": Filecount });
                            localStorage.setItem('itemList', JSON.stringify(itemObject));
                            alert(localStorage.getItem('itemList'));
                        }
                    });

                        if (file.size < 5 * 1024 * 1024 && file.type.match('pdf.*')) {
                            alert('File added - ' + file.name + ' - Size - ' + file.size);
                            if (typeof Filecount !== 'undefined' && Filecount != '') {
                                Filecount = parseFloat(Filecount + file.size);
                            }
                                else {
                                Filecount = parseFloat(file.size);
                            }


                            var storage = localStorage.getItem('itemList') || '{"items":[]}';
                            var itemObject = JSON.parse(storage);
                            itemObject["items"].push({ "FileTypeID": $('#LKP_DocType').find(":selected").val(), "FileTotalSize": Filecount });
                            localStorage.setItem('itemList', JSON.stringify(itemObject));
                            alert(localStorage.getItem('itemList'));
}

In this code I have limitation for File Size and File PDF only. I have LKP_DocType dropdown to select file type. Now from dropdown i will select file type and i can only select max of 5MB files per File type from drop down.

Later I am adding FileTypeID and File Size in array.

I want to check if File Type ID alrady exist just replace size value in array other wise if File type is not in Array add value in array

Hopes for your suggestions

Thanks

0 Answers
Related