54 lines
2.1 KiB
Vue
54 lines
2.1 KiB
Vue
<template>
|
||
<div class="aa-upload">
|
||
<div class="aa-upload-drop" :class="{ 'is-over': over }" @click="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 setup>
|
||
import { ref } from 'vue';
|
||
|
||
const files = ref([]);
|
||
const over = ref(false);
|
||
const file = ref(null);
|
||
|
||
function addFiles(flist) {
|
||
Array.prototype.forEach.call(flist, (f) => {
|
||
const id = 'f' + Date.now() + Math.random().toString(36).slice(2, 6);
|
||
const item = { id, name: f.name, progress: 0, done: false, thumb: '' };
|
||
if (/^image\//.test(f.type) && f.size < 2 * 1024 * 1024) {
|
||
const reader = new FileReader();
|
||
reader.onload = (e) => (item.thumb = e.target.result);
|
||
reader.readAsDataURL(f);
|
||
}
|
||
files.value.push(item);
|
||
simulate(item);
|
||
});
|
||
}
|
||
function onChange(e) { addFiles(e.target.files); e.target.value = ''; }
|
||
function onDrop(e) { over.value = false; addFiles(e.dataTransfer.files); }
|
||
function simulate(item) {
|
||
const t = setInterval(() => {
|
||
item.progress = Math.min(100, item.progress + Math.random() * 22);
|
||
if (item.progress >= 100) { item.progress = 100; item.done = true; clearInterval(t); }
|
||
}, 220);
|
||
}
|
||
function remove(id) { files.value = files.value.filter((x) => x.id !== id); }
|
||
</script>
|
||
|
||
<style src="./DragUpload.css"></style>
|