简单实现ajax拖拽上传文件
程序员文章站
2022-03-07 16:46:49
ajax拖拽上传功能实现,供大家参考,具体内容如下
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;
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: ps怎么给平静的海面制作波涛汹涌的感觉?
下一篇: 怎样泡姜茶最好喝,以及有什么作用与功效