Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filesize is compared in KB (= 1000 Bytes) but displayed in KiB (= 1024 Bytes). #1736

Closed
9 of 10 tasks
reckert opened this issue Jul 20, 2021 · 1 comment
Closed
9 of 10 tasks

Comments

@reckert
Copy link

reckert commented Jul 20, 2021

Prerequisites

  • I have searched for similar issues in both open and closed tickets and cannot find a duplicate.
  • The issue still exists against the latest master branch of bootstrap-fileinput.
  • This is not an usage question. I confirm having read the plugin documentation and demos.
  • This is not a general programming / coding question. (Those should be directed to the webtips Q & A forum).
  • I have attempted to find the simplest possible steps to reproduce the issue.

Steps to reproduce the issue

  1. Create a simple file upload form. Set the maxFileSize to 100 (KB/KiB?). Open it in your browser (even from the file system).
	$(document).ready(function() {
		$("#input-upload").fileinput({
			maxFileSize: 100
		});
	});
  1. Or try it here: https://jsfiddle.net/1neawk5x/
  2. Drop a file with a size of (i.e.) 101974 bytes (= 99.58 KiB = 101.97 KB)
  3. Look at the file size in the preview and in the error message

Expected behavior and actual behavior

The preview shows that the size of the file is 99.5 KB. That is identical to the value shown in Ms Windows file explorer.
The error message reports "File "xxxx" (101.97 KB) exceeds maximum allowed upload size of 100 KB.

I was expecting no error message to occur / both values to be the same.

When a file is selected, its size is converted into KB by dividing by 1000.

caption = self._getFileName(file, ''), fileSize = (file && file.size || 0) / 1000,

This value is later compared to maxFileSize, so it can be assumed that maxFileSize holds the maximum size in KB (1000 bytes), too

if (self.maxFileSize > 0 && fileSize > self.maxFileSize) {

The default method to display the size really uses KiB (1024 bytes), though.

out = (size / Math.pow(1024, i)).toFixed(2) + ' ' + sizes[i];

That way, the size compared to the maxFileSize property is a different one than the one displayed to the user.

The "iB" units are not frequently used by normal users and several systems (including MS Windows file explorer) calculate in Base2 (1024 bytes) but still show "KB", "MB", etc..
I would suggest to just change the calculation of the file size by changing 1000 to1024 in line 5379 in js/fileinput.js.

Environment

Browsers

  • any, tested on Firefox Desktop and Chrome

Operating System

  • any, tested on MS Window

Libraries

  • jQuery version: 3.6.0
  • bootstrap-fileinput version: 5.2.2

Isolating the problem

  • This bug happens on the plugin demos page
  • The bug happens consistently across all tested browsers
  • This bug happens when using bootstrap-fileinput without other plugins
@kartik-v
Copy link
Owner

kartik-v commented Jul 20, 2021

Fixed via the latest commit, New property bytesToKB has been introduced (and can be modified if needed) which defaults to 1024 and is used for conversion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants