-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathupload.html
93 lines (90 loc) · 4.1 KB
/
upload.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<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">
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/styles/iview.css">
<script src="https://unpkg.com/[email protected]/dist/vue.min.js"></script>
<script src="https://unpkg.com/[email protected]/dist/iview.min.js"></script>
<title>文件上传服务</title>
</head>
<body>
<div id="app">
<card>
<span>上传文件到
<i-select v-model="selectedDir" style="width:200px">
<i-option v-for="dir in dirs" :value="dir" :key="dir">{{ dir }}</i-option>
</i-select>目录</span>
<i-button type="primary" @click="redirect" style="margin-left: 30px">访问目录</i-button>
</card>
<upload
multiple
type="drag"
:action="uploadUrl"
:before-upload="beforeUpload">
<div style="padding: 20px 0">
<icon type="ios-cloud-upload" size="52" style="color: #3399ff"></icon>
<p>点击或拖拽文件上传</p>
</div>
</upload>
</div>
<script>
function ajax(option) {
return new Promise((resolve, reject) => {
let xhr = new XMLHttpRequest()
xhr.open(option.method || 'GET', option.url)
xhr.onload = () => {
let data = JSON.parse(xhr.responseText)
resolve(data)
}
xhr.onerror = error => {
reject(error)
}
xhr.send(option.data ? JSON.stringify(option.data) : null)
})
}
new Vue({
el: '#app',
data: {
dirs: [
'/',
'/tmp'
],
selectedDir: '/',
uploadUrl: '',
uploadApi: '/_upload'
},
mounted() {
// this.listdir()
},
methods: {
redirect: function() {
if (this.selectedDir !== '/') {
window.location.href = this.selectedDir + '/'
} else {
window.location.href = this.selectedDir
}
},
async listdir() {
this.dirs = await ajax({
url: '/_listdir'
})
},
beforeUpload: function() {
if (this.selectedDir !== '/') {
this.uploadUrl = this.uploadApi + this.selectedDir
} else {
this.uploadUrl = this.uploadApi
}
let promise = new Promise((resolve) => {
this.$nextTick(function () {
resolve(true);
})
})
return promise
}
}
})
</script>
</body>
</html>