Skip to content

Commit aee5382

Browse files
committed
拖放上传文件
1 parent e38a728 commit aee5382

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ easy file manager
1212
可以自定义前端HTML代码,只需要修改templates目录下的文件即可
1313
目前实现的功能:
1414
- 后台远程下载
15-
- 上传文件
15+
- 上传文件&拖放上传
1616
- 删除文件&文件夹
1717
- 新建文件&文件夹
1818
- 解压ZIP、gz压缩包

templates/index.tmpl

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,56 @@
5151
<a href="#" onclick="createNewDir()">新建文件夹</a>
5252
<script src="https://s3.pstatp.com/cdn/expire-1-M/jquery/3.2.1/jquery.min.js"></script>
5353
<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
54104
function createNewDir() {
55105
const dirName = window.prompt("请输入文件夹名:");
56106
if (dirName !== null) {

0 commit comments

Comments
 (0)