Jen*_*ell 13 jquery html5 drag-and-drop
我已经看了很多脚本,用于通过拖放式AJAX将图像上传到服务器.我找到的脚本不是jQuery,非常大,并没有完全按照我的意愿行事.
将来它应该使用jQuery,AJAX和PHP上传图像.
我的问题
在许多示例中,我查看了e.dataTransfer.files的工作.在我的情况下,它没有.我需要以某种方式绑定它吗?
我想尽可能多地使用jQuery.
的jsfiddle
尽情玩耍......
码
<html>
<head>
<style type="text/css">
#dropzone {
border: 2px dashed #ccc;
width: 300px;
height: 200px;
}
</style>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('#dropzone').on({
dragenter: function(e) {
$(this).css('background-color', 'lightBlue');
},
dragleave: function(e) {
$(this).css('background-color', 'white');
},
drop: function(e) {
e.stopPropagation();
e.preventDefault();
console.log(e.dataTransfer.files);
}
});
});
</script>
</head>
<body>
<div id="dropzone">
Drop files here
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
Ron*_*den 15
我为我的申请写了一个扩展名.
// Custom file drop extension
$.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) {
var reader = new FileReader()
reader.onload = function(event) {
options.callback(event.target.result)
}
reader.readAsDataURL(files[0])
}
return false
})
})
}
})
Run Code Online (Sandbox Code Playgroud)
我们可以像这样使用它
// Event listener filedropper
$('.dropbox').filedrop({
callback : function(fileEncryptedData) {
// a callback?
}
})
Run Code Online (Sandbox Code Playgroud)
如果你想删除多个文件,你应该在FileReader周围写一个for循环,如下所示:
...
if(options.callback) {
for (i = 0; i < files.length; i++) {
var reader = new FileReader()
reader.onload = function(event) {
options.callback(event.target.result)
}
reader.readAsDataURL(files[0])
}
}
...
Run Code Online (Sandbox Code Playgroud)
JSFiddle:http://jsfiddle.net/646xe1m2/
我发现它是jQuery.1.8中的一个错误.这行应该在之前$('.dropzone').
$.event.props.push('dataTransfer');
Run Code Online (Sandbox Code Playgroud)
最终的HTML代码
<html>
<head>
<style type="text/css">
.dropzone {
border: 2px dashed #ccc;
width: 300px;
height: 200px;
}
</style>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script type="text/javascript">
jQuery(document).ready(function($) {
var filename = '';
var image_data = '';
$.event.props.push('dataTransfer');
$('.dropzone').on({
dragenter: function(e) {
$(this).css('background-color', 'lightBlue');
},
dragleave: function(e) {
$(this).css('background-color', 'white');
},
drop: function(e) {
e.stopPropagation();
e.preventDefault();
var file = e.dataTransfer.files[0];
var fileReader = new FileReader();
var this_obj = $(this);
fileReader.onload = (function(file) {
return function(event) {
// Preview
filename = file.name;
image_data = event.target.result;
$(this_obj).next().html('<a href="#" class="upload-file">Upload file</a>');
$(this_obj).html('<img style="max-width: 200px; max-height: 200px;" src="' + event.target.result + '">');
};
})(file);
fileReader.readAsDataURL(file);
}
});
// Upload file
$(".upload-file").live("click", function(e){
e.preventDefault();
var this_obj = $(this);
var image_data = $(this_obj).parent().prev().find('img').attr('src');
$.post(
'send_image.php',
{
data: image_data,
filename: filename
}, function(response){
$(this_obj).parent().prev().html(response);
$(this_obj).remove();
}
);
//console.log('ok');
});
});
</script>
</head>
<body>
<!-- Multiple dropzones -->
<div class="dropzone">
Drop files here
</div>
<div id="meta"></div>
<div class="dropzone">
Drop files here
</div>
<div id="meta"></div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
send_image.php中的PHP代码
<?php
$raw_data = $_POST['data'];
file_put_contents(
'image123.jpg',
base64_decode(
str_replace('data:image/jpeg;base64,', '', $raw_data)
)
);
?>
<br>
<?php echo '<img style="max-width: 200px; max-height: 200px;" src="' . 'image123.jpg' . '">'; ?>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
31061 次 |
| 最近记录: |