Skip to content

Instantly share code, notes, and snippets.

@wemrekurt
Created January 27, 2019 14:59
Show Gist options
  • Select an option

  • Save wemrekurt/e09613afeb2a221bf829728c7ab0878e to your computer and use it in GitHub Desktop.

Select an option

Save wemrekurt/e09613afeb2a221bf829728c7ab0878e to your computer and use it in GitHub Desktop.
Drag-drop file uploader
$.fn.extend({
filedrop: function (options) {
var defaults = {
callback : null
}
options = $.extend(defaults, options)
return this.each(function() {
var files = []
var $this = $(this)
// Stop default browser actions
$this.bind('dragover dragleave', function(event) {
event.stopPropagation();
event.preventDefault();
})
// Catch drop event
$this.bind('drop', function(event) {
// Stop default browser actions
event.stopPropagation()
event.preventDefault()
// Get all files that are dropped
files = event.originalEvent.target.files || event.originalEvent.dataTransfer.files
// Convert uploaded file to data URL and pass trought callback
if(options.callback) {
for (i = 0; i < files.length; i++) {
options.callback(files[i])
}
}
return false
})
})
}
// KULLANIMI
/*$('#dropbox').filedrop({
callback : function(fileEncryptedData) {
console.log(fileEncryptedData)
}
})
*/
})
if (checkFile(fileEncryptedData['type'])) {
if (i < max) {
updateProgress(true)
$($('.item')[i]).fadeIn()
$($('.item')[i]).find('.desc').html('Bekleniyor.')
var fd = new FormData();
fd.append("tmp_image[file]", fileEncryptedData);
var xhr = new XMLHttpRequest();
xhr.open('POST', "#{membership_product_tmp_image_path}.json")
xhr.upload.onprogress = function (e) {
if (e.lengthComputable) {
var percentComplete = (e.loaded / e.total) * 100;
$($('.item')[j]).find('.desc').html('%' + Math.round(percentComplete) + ' tamamlandı')
if (percentComplete == 100)
j++
}
}
xhr.onload = function () {
if (this.status == 200) {
var resp = JSON.parse(this.response);
if (resp['success']) {
$($('.item')[k]).attr('data-uploaded', 'true')
$($('.item')[k]).find('img').attr('src', resp['image']['file']['url'])
$($('.item')[j]).find('.desc').html('Yükleme Tamamlandı')
$($('.item')[k]).find('.tmp_input').html('<input type="hidden" name="tmp_images[]" value="' + resp['image']['id'] + '" />')
k++
} else {
updateProgress(false)
showSwal("Oops!", "Yüklenemedi.", "error");
}
}
}
xhr.send(fd)
i++
} else {
$('#dropbox').addClass('disabled');
}
} else {
showSwal("Oops!", "Hatalı dosya türü. Yalnızca jpeg ve png.", "error");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment