function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Walter@AdicioWalter@Adicio 

Can you check why im off a little when calculating inputFile size?

for example when i upload a file that is 4.89 KB according to windows file properties, my output below gives me 4.74 KB. Its always real close and a little under.

 

 

String s = Document.body.toString();

Decimal byteCount = s.length();

Decimal kb_dec = byteCount / 1000;
Decimal kb_divDec = byteCount.divide(1000, 2, System.RoundingMode.UP);
	   			
double mb = kb_dec / 1000;
Decimal mb_divDec = kb_dec.divide(1000, 2, System.RoundingMode.UP);
	   			
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Info,'The image size is... ' +mb_divDec +' MBs, ' +kb_divDec+' KBs, '+byteCount+' bytes.'));

 

 

Best Answer chosen by Admin (Salesforce Developers) 
SuperfellSuperfell

1KB is 1024 bytes, not 1000 bytes. (unless you manufacture disk drives)

All Answers

SuperfellSuperfell

1KB is 1024 bytes, not 1000 bytes. (unless you manufacture disk drives)

This was selected as the best answer
Walter@AdicioWalter@Adicio

Thank you SimonF. i also couldnt get it right until using body.size() instead of getting the string length() of the body.

 

 

Decimal byteCount = Attachment.body.size();

Decimal kb_dec = byteCount / 1024;
Decimal kb_divDec = byteCount.divide(1024, 1, System.RoundingMode.UP);
	   			
Decimal mb = kb_dec / 1024;
Decimal mb_divDec = kb_dec.divide(1024, 1, System.RoundingMode.UP);
	   			
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Info,'The image size is... ' +mb_divDec +' MBs, ' +kb_divDec+' KBs, '+byteCount+' bytes.'));