Files
aurora-admin/frameworks/DragUpload.vue2.vue
T

57 lines
2.2 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<template>
<div class="aa-upload">
<div class="aa-upload-drop" :class="{ 'is-over': over }" @click="$refs.file.click()" @dragover.prevent="over = true" @dragleave="over = false" @drop.prevent="onDrop">
<span class="aa-upload-icon">⬆️</span>
<div class="aa-upload-hint">将文件拖到此处,或 <b>点击选择</b></div>
</div>
<input ref="file" type="file" multiple style="display:none" @change="onChange" />
<div class="aa-upload-list">
<div v-for="it in files" :key="it.id" class="aa-upload-item">
<div class="aa-upload-thumb"><img v-if="it.thumb" :src="it.thumb" /><span v-else>文件</span></div>
<div class="aa-upload-meta">
<div class="aa-upload-name">{{ it.name }}</div>
<div class="aa-upload-bar"><i :style="{ width: it.progress + '%' }"></i></div>
</div>
<div class="aa-upload-status">{{ it.done ? '已完成' : Math.round(it.progress) + '%' }}</div>
<button class="aa-upload-del" @click="remove(it.id)">×</button>
</div>
</div>
</div>
</template>
<script>
export default {
name: 'AaDragUpload',
data() {
return { files: [], over: false };
},
methods: {
addFiles(flist) {
var self = this;
Array.prototype.forEach.call(flist, function (f) {
var id = 'f' + Date.now() + Math.random().toString(36).slice(2, 6);
var item = { id: id, name: f.name, progress: 0, done: false, thumb: '' };
if (/^image\//.test(f.type) && f.size < 2 * 1024 * 1024) {
var reader = new FileReader();
reader.onload = function (e) { item.thumb = e.target.result; };
reader.readAsDataURL(f);
}
self.files.push(item);
self.simulate(item);
});
},
onChange(e) { this.addFiles(e.target.files); e.target.value = ''; },
onDrop(e) { this.over = false; this.addFiles(e.dataTransfer.files); },
simulate(item) {
var t = setInterval(function () {
item.progress = Math.min(100, item.progress + Math.random() * 22);
if (item.progress >= 100) { item.progress = 100; item.done = true; clearInterval(t); }
}, 220);
},
remove(id) { this.files = this.files.filter(function (x) { return x.id !== id; }); }
}
};
</script>
<style src="./DragUpload.css"></style>