Can we show an error message in MB regarding the fileinput plugin using the msgSizeTooLarge parameter

As for the file input plugin ( http://plugins.krajee.com/file-input ).

Can we show a file size error message in MB? It is possible to customize the error message, but I could not find the size in MB instead of KB.

msgSizeTooLarge

The file "{name}" ({size} KB) exceeds the maximum download size {maxSize} KB. Try uploading again!

Any solution to display size in MB?

+4
source share
1 answer

, Bootstrap File Input . , Javascript!

, fileuploaderror (, ). , , . ). msgSizeTooLarge , . msgSizeTooLarge ; {size} {customSize}, {maxSize} {customMaxSize}. msgSizeTooLarge :

msgSizeTooLarge: 'File "{name}" (<b>{customSize}</b>) exceeds maximum allowed upload size of <b>{customMaxSize}</b>. Please retry your upload!'

, data, fileuploaderror, size = data.files[0].size. maxFileSize , filenputs maxFileSize = $(this).data().fileinput.maxFileSize. , MB . formatSize wil :

formatSize = (s) => {
    i = Math.floor(Math.log(s) / Math.log(1024));
    sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
    out = (s / Math.pow(1024, i)).toFixed(2) * 1 + ' ' + sizes[i];
    return out;
};

replace :

msg = msg.replace('{customSize}', formatSize(size));
msg = msg.replace('{customMaxSize}', formatSize(maxFileSize * 1024 /* Convert KB to Bytes */));

:

"Endless Dream.mp3" (2.07 ) 2 . !

, data.id, <li> , :

$('li[data-file-id="'+data.id+'"]').html(msg);

:

Bootrstrap file login screenshot

. 2 .

javascript

$("#file-1").fileinput({
    uploadUrl: '#', // you must set a valid URL here else you will get an error
    overwriteInitial: false,
    maxFileSize: 2048, // 2028 KB == 2 MB
    msgSizeTooLarge: 'File "{name}" (<b>{customSize}</b>) exceeds maximum allowed upload size of <b>{customMaxSize}</b>. Please retry your upload!'
}).on('fileuploaderror', function(event, data, msg) {
    var size = data.files[0].size,
        maxFileSize = $(this).data().fileinput.maxFileSize,
        formatSize = (s) => {
            i = Math.floor(Math.log(s) / Math.log(1024));
            sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
            out = (s / Math.pow(1024, i)).toFixed(2) * 1 + ' ' + sizes[i];
            return out;
        };

    msg = msg.replace('{customSize}', formatSize(size));
    msg = msg.replace('{customMaxSize}', formatSize(maxFileSize * 1024 /* Convert KB to Bytes */));
    $('li[data-file-id="'+data.id+'"]').html(msg);
});
0

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


All Articles