|
51 | 51 | <a href="#" onclick="createNewDir()">新建文件夹</a> |
52 | 52 | <script src="https://s3.pstatp.com/cdn/expire-1-M/jquery/3.2.1/jquery.min.js"></script> |
53 | 53 | <script> |
| 54 | + //拖放上传文件 start |
| 55 | + // 获取 body 元素 |
| 56 | + const body = document.querySelector('body'); |
| 57 | + |
| 58 | + // 监听文件拖放事件 |
| 59 | + body.addEventListener('drop', (event) => { |
| 60 | + event.preventDefault(); |
| 61 | + event.stopPropagation(); |
| 62 | + |
| 63 | + // 获取拖放的文件 |
| 64 | + const file = event.dataTransfer.files[0]; |
| 65 | + |
| 66 | + // 判断拖放的元素是否为文件,如果是,则模拟表单提交 |
| 67 | + if (file) { |
| 68 | + // 创建 form 元素 |
| 69 | + const form = document.createElement('form'); |
| 70 | + form.setAttribute('method', 'post'); |
| 71 | + form.setAttribute('enctype', 'multipart/form-data'); |
| 72 | + form.setAttribute('action', '/do/upload/{{.path}}'); |
| 73 | + |
| 74 | + // 创建 file input 元素 |
| 75 | + const fileInput = document.createElement('input'); |
| 76 | + fileInput.setAttribute('type', 'file'); |
| 77 | + fileInput.setAttribute('name', 'file'); |
| 78 | + fileInput.files = event.dataTransfer.files; |
| 79 | + |
| 80 | + // 创建 submit input 元素 |
| 81 | + const submitInput = document.createElement('input'); |
| 82 | + submitInput.setAttribute('type', 'submit'); |
| 83 | + submitInput.setAttribute('value', '提交'); |
| 84 | + |
| 85 | + // 添加元素到 form 中 |
| 86 | + form.appendChild(fileInput); |
| 87 | + form.appendChild(submitInput); |
| 88 | + |
| 89 | + // 添加 form 到 body 中 |
| 90 | + body.appendChild(form); |
| 91 | + |
| 92 | + // 提交表单 |
| 93 | + form.submit(); |
| 94 | + } |
| 95 | + }); |
| 96 | + |
| 97 | + // 防止浏览器默认行为 |
| 98 | + body.addEventListener('dragover', (event) => { |
| 99 | + event.preventDefault(); |
| 100 | + event.stopPropagation(); |
| 101 | + }); |
| 102 | + |
| 103 | + //拖放上传文件 end |
54 | 104 | function createNewDir() { |
55 | 105 | const dirName = window.prompt("请输入文件夹名:"); |
56 | 106 | if (dirName !== null) { |
|
0 commit comments