当前位置: 移动技术网 > IT编程>脚本编程>Ajax > 简单实现ajax拖拽上传文件

简单实现ajax拖拽上传文件

2018年04月26日  | 移动技术网IT编程  | 我要评论

ajax拖拽上传功能实现,供大家参考,具体内容如下

<!doctype html>
<html lang="en">

<head>
 <meta charset="utf-8">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <meta http-equiv="x-ua-compatible" content="ie=edge">
 <title>document</title>
 <style>
 .box {
  width: 300px;
  height: 300px;
  border: 1px solid #000;
  text-align: center;
  line-height: 300px;
  font-size: 40px;
 }
 </style>
</head>

<body>
 <div class="box">+</div>

 <script>
 var box = document.queryselector('.box');

 box.ondragover = function (e) {
  e.preventdefault();
 }
 box.ondrop = function (e) {
  console.log(e.datatransfer)
  e.preventdefault();
  var xhr = new xmlhttprequest();
  xhr.onreadystatechange = function () {
  if (xhr.readystate == 4 && xhr.status == 200) {
   console.log(xhr.responsetext)
  }
  }
  xhr.open('post', './server.php', true);

  var formdata = new formdata();
  formdata.append('pic', e.datatransfer.files[0]);
  formdata.append('name', 'luyao');
  xhr.send(formdata);

 }
 </script>
</body>

</html>

//server.php

<?php
 $rand = rand(1,1000).'.jpg';
 move_uploaded_file($_files['pic']['tmp_name'], './uploads/'.$rand);
 echo '/uploads/'.$rand;

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网