-
-
Save mygoare/18ac2aa6c5253090b9f6d4c4cd44be60 to your computer and use it in GitHub Desktop.
drag and drop uploads
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (isAdvancedUpload) { | |
var droppedFiles = false; | |
$form.on('drag dragstart dragend dragover dragenter dragleave drop', function(e) { | |
e.preventDefault(); | |
e.stopPropagation(); | |
}) | |
.on('dragover dragenter', function() { | |
$form.addClass('is-dragover'); | |
}) | |
.on('dragleave dragend drop', function() { | |
$form.removeClass('is-dragover'); | |
}) | |
.on('drop', function(e) { | |
droppedFiles = e.originalEvent.dataTransfer.files; | |
}); | |
} | |
if (isAdvancedUpload) { | |
e.preventDefault(); | |
var ajaxData = new FormData($form.get(0)); | |
if (droppedFiles) { | |
$.each( droppedFiles, function(i, file) { | |
ajaxData.append( $input.attr('name'), file ); | |
}); | |
} | |
$.ajax({ | |
url: $form.attr('action'), | |
type: $form.attr('method'), | |
data: ajaxData, | |
dataType: 'json', | |
cache: false, | |
contentType: false, | |
processData: false, | |
complete: function() { | |
$form.removeClass('is-uploading'); | |
}, | |
success: function(data) { | |
$form.addClass( data.success == true ? 'is-success' : 'is-error' ); | |
if (!data.success) $errorMsg.text(data.error); | |
}, | |
error: function() { | |
// Log the error, show an alert, whatever works for you | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
references: https://css-tricks.com/drag-and-drop-file-uploading/