前端之家收集整理的这篇文章主要介绍了
ajax 文件拖拽上传,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
客户端
<!DOCTYPE html>
<html lang="en">
<head>
<Meta charset="UTF-8">
<title>Title</title>
<style> #filePost{ width: 300px; height: 300px; border: 1px #F00 solid; } </style>
</head>
<body>
<div id="filePost" ondrop="drop(event)"></div>
<script> document.ondrop=function(event){ event.preventDefault(); }; document.ondragover=function(event){ event.preventDefault(); }; document.ondragenter=function(event){ event.preventDefault(); }; function drop(event){ var files=event.dataTransfer.files; if(files.length==0){ return false; } if(!files[0].type.match(/image*/)){ alert("不是图片") return false; } var xhr=new XMLHttpRequest(); xhr.open("post","upload.PHP",true); var formData=new FormData(); formData.append('mypic',files[0]); xhr.send(formData); } </script>
</body>
</html>
服务器端
<?PHP
/** * Created by PHPStorm. * User: Administrator * Date: 2016/7/23 * Time: 10:51 */
if(!empty($_FILES["mypic"])){
move_uploaded_file($_FILES["mypic"]["tmp_name"],$_FILES["mypic"]["name"]);
}
?>
原文链接:https://www.f2er.com/ajax/161946.html