Frage

I have this code to get the size of an uploaded file:

var iSize = ($("#formmedia")[0].files[0].size / 1024); 
if (iSize / 1024 > 1) 
{ 
if (((iSize / 1024) / 1024) > 1) 
{ 
    iSize = (Math.round(((iSize / 1024) / 1024) * 100) / 100);
    $("#size").html( iSize + "Gb"); 
}
else
{ 
    iSize = (Math.round((iSize / 1024) * 100) / 100)
    $("#size").html( iSize + "Mb"); 
} 
} 
else 
{
iSize = (Math.round(iSize * 100) / 100)
$("#size").html( iSize  + "kb"); 
}

This code works completely fine but it shows the output as:

<div id="size">5.78 Mb</div>

How can I get it to ALWAYS ONLY show Kilobytes?

War es hilfreich?

Lösung

Just remove the parts that check if its >1MB or >1GB, and you're left with:

var iSize = ($("#formmedia")[0].files[0].size / 1024); 
iSize = (Math.round(iSize * 100) / 100)
$("#size").html( iSize  + "kb"); 

Andere Tipps

Simply remove all but:

var iSize = ($("#formmedia")[0].files[0].size / 1024); 
iSize = (Math.round(iSize * 100) / 100)
$("#size").html( iSize  + "kb"); 

So that the size is simply converted to kb regardless of how large the file is.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top