Skip to content

Instantly share code, notes, and snippets.

@andy0130tw
Created June 1, 2014 16:20
Show Gist options
  • Save andy0130tw/c9cf37f5556c4a87736c to your computer and use it in GitHub Desktop.
Save andy0130tw/c9cf37f5556c4a87736c to your computer and use it in GitHub Desktop.
HTML Drag'n'Drop Quick Example
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script>
function noop(e){
//e.stopPropagation();//取消drag預設動作
e.preventDefault();//取消drag預設動作
}
$(function(){
var ctrl=$('#upload')[0];
ctrl.addEventListener("dragenter",noop,false);
ctrl.addEventListener("dragexit",noop,false);
ctrl.addEventListener("dragover",noop,false);
ctrl.addEventListener("drag",function(e){
noop(e);
console.log("drag!");
},false);
ctrl.addEventListener("drop",function(e){
noop(e);
var files = e.dataTransfer.files;
console.log(files[0]);
var formdata = new FormData();
formdata.append('image',files[0]);
$.ajax({
type:'POST',
data:formdata,
dataType:'text',
processData: false,
contentType: false,
url: 'upload.php',
success: function(data){alert(data)}
});
},false);
});
</script>
<style>
#upload{
height: 400px;
background-color: #ccc;
}
</style>
</head>
<body>
<div id="upload"></div>
</body>
</html>
<?php
if(isset($_FILES["image"])){
if($_FILES["image"]["error"]>0){
print("Error occured while uploading!");
}else{
//$imageConfig = array('w'=>100);
//$data["image"]="@".resize($_FILES["image"]["tmp_name"],$imageConfig).";type=image/jpeg";
print("The file is saved to: ".$_FILES["image"]["tmp_name"]);
}
}else{
print("No file sent\n");
print_r($_FILES);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment