(^316) ❘ CHAPTER 11 HTML5 DRAG AND DROP
console.log(
'This browser does not support HTML 5 ' +
'drag and drop file uploads.'
);
this.closeProgressDialogue();
}
},
Additional Utilities
The remaining methods in the JavaScript act as utilities for simplifying the remaining actions of size
calculation, string manipulation, and event application.The getFileSize() method returns a human-readable representation of fi le size. The fi le size in bytes
is fed into the method, and a number representing the fi le size in bytes, kilobytes (KB), megabytes
(MB), or gigabytes (GB) is returned. This method uses the Math.pow() method, where Math.
pow(2,10) = 1 KB, Math.pow(2,20) = 1 MB, and Math.pow(2,30) = 1 GB.getFileSize : function(bytes)
{
switch (true)
{
case (bytes < Math.pow(2,10)):
{
return bytes + ' Bytes';
}
case (bytes >= Math.pow(2,10) && bytes < Math.pow(2,20)):
{
return Math.round(
bytes / Math.pow(2,10)
) +' KB';
}
case (bytes >= Math.pow(2,20) && bytes < Math.pow(2,30)):
{
return Math.round(
(bytes / Math.pow(2,20)) * 10
) / 10 + ' MB';
}
case (bytes > Math.pow(2,30)):
{
return Math.round(
(bytes / Math.pow(2,30)) * 100
) / 100 + ' GB';
}
}
},The applyEvents() method applies all the drag-and-drop events that you fi rst implemented
in Example 11-1. You start the method by creating a context variable, which decides how the
events are applied. If you apply the events to a fi le or folder object that has been dragged and
dropped from outside the browser window, this method applies each drag-and-drop event to