Web Development with jQuery®

(Elliott) #1

Implementing Drag-and-Drop File Uploads (^) ❘ 303
if (typeof FormData !== 'undefined')
{
var form = new FormData();
// The form object invoked here is a built-in object, provided
// by the browser; it allows you to specify POST variables
// in the request for the file upload.
form.append('path', this.path);
$(this.files).each(
function(key, file)
{
form.append('file[]', file);
form.append('name[]', file.name);
form.append('replaceFile[]', 1);
}
);
// This sends a POST request to the server at the path
// /file/upload.php. This is the server-side file that will
// handle the file upload.
this.http.open('POST', 'file/upload.json');
this.http.send(form);
}
else
{
console.log(
'This browser does not support HTML 5 ' +
'drag and drop file uploads.'
);
this.closeProgressDialogue();
}
},
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';
http://www.it-ebooks.info

Free download pdf