-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #553 from beansmile/feat-drag-upload
feat: 支持拖拽上传
- Loading branch information
Showing
11 changed files
with
227 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
<template> | ||
<div | ||
class="admin-dropbox" | ||
:class="{ active, disabled }" | ||
@dragenter.stop.prevent="handleDropEnter" | ||
@dragover.stop.prevent="handleDropEnter" | ||
@dragleave.stop.prevent="handleDropLeave" | ||
@drop.stop.prevent="handleDropFile" | ||
@click.stop="handleClick" | ||
v-loading="loading" | ||
> | ||
<i class="el-icon-upload" /> | ||
<div class="upload-text">{{ $t('bean.dragAndDropUpload') }}</div> | ||
<slot /> | ||
|
||
<input | ||
:accept="accept" | ||
type="file" | ||
style="position: fixed; left: -200%; width: 0; height: 0;" | ||
ref="input" | ||
:multiple="limit > 1" | ||
@change="handleFileInputChange" | ||
@click.stop | ||
/> | ||
</div> | ||
</template> | ||
|
||
<script> | ||
import { Vue, Component, Prop } from 'vue-property-decorator'; | ||
@Component | ||
export default class DropBox extends Vue { | ||
@Prop(String) accept; | ||
@Prop(Boolean) disabled; | ||
@Prop(Boolean) loading; | ||
@Prop({ type: Number, default: 0 }) size; // 单位M 0表示不限制 | ||
@Prop({ type: Number, default: 1 }) limit; | ||
active = false; | ||
checkFileType(file, accept) { | ||
if (!accept || accept === '*') { | ||
return true; | ||
} | ||
const valid = accept.replace(/\s/g, '').split(',').filter(accept => { | ||
if (accept.startsWith('.')) { | ||
return (file.name || '').toLowerCase().endsWith(accept.toLowerCase()); | ||
} | ||
return new RegExp(accept.replace('*', '.*')).test(file.type); | ||
}).length > 0; | ||
return valid; | ||
} | ||
handleDropEnter() { | ||
this.active = true; | ||
} | ||
handleDropLeave() { | ||
this.active = false; | ||
} | ||
validateFile(file, { validateType = true, validateSize = true } = {}) { | ||
if (validateType) { | ||
const fileTypeValid = this.checkFileType(file, this.accept); | ||
if (!fileTypeValid) { | ||
return `${file.name} - ${this.$t('bean.fileTypeisInvalid')}`; | ||
} | ||
} | ||
if (validateSize && this.size > 0) { | ||
if (file.size > this.size * 1024 * 1024) { | ||
return `${file.name} - ${this.$t('bean.maximumFileSize', { size: `${this.size}M` })}`; | ||
} | ||
} | ||
return null; | ||
} | ||
handleFiles(files, { validateType = true, validateSize = true } = {}) { | ||
if (files.length) { | ||
if (files.length > this.limit) { | ||
this.$emit('error', this.$t('bean.maximumUploadFileCount', { count: this.limit })); | ||
return; | ||
} | ||
if (this.limit === 1) { | ||
const errMsg = this.validateFile(files[0], { validateType, validateSize }); | ||
if (errMsg) { | ||
this.$emit('error', errMsg); | ||
return; | ||
} | ||
this.$emit('change', files); | ||
} else { | ||
const validFiles = files.filter(item => !this.validateFile(item, { validateType, validateSize })); | ||
if (validFiles.length !== files.length) { | ||
this.$emit('error', this.$t('bean.filteredUploadFileTip')); | ||
} | ||
if (validFiles.length) { | ||
this.$emit('change', validFiles); | ||
} | ||
} | ||
} | ||
} | ||
handleDropFile(e) { | ||
this.handleDropLeave(); | ||
this.handleFiles([...e.dataTransfer.files]); | ||
} | ||
handleFileInputChange(e) { | ||
const files = e.target.files; | ||
this.handleFiles([...files], { validateType: false }); | ||
// 相同文件change事件不会触发 | ||
e.target.value = ''; | ||
} | ||
handleClick() { | ||
this.$refs.input.click(); | ||
} | ||
} | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
.admin-dropbox { | ||
background-color: #fff; | ||
border: 2px dashed #d9d9d9; | ||
border-radius: 6px; | ||
box-sizing: border-box; | ||
width: 360px; | ||
height: 180px; | ||
text-align: center; | ||
cursor: pointer; | ||
overflow: hidden; | ||
|
||
&.disabled { | ||
pointer-events: none; | ||
opacity: .3; | ||
} | ||
|
||
&:not(.disabled) { | ||
&.active, | ||
&:hover { | ||
border-color: $--color-primary; | ||
} | ||
} | ||
|
||
.el-icon-upload { | ||
font-size: 67px; | ||
color: #c0c4cc; | ||
margin: 40px 0 16px; | ||
line-height: 50px; | ||
} | ||
|
||
.upload-text { | ||
color: #606266; | ||
font-size: $--font-size-base; | ||
text-align: center; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,6 +58,11 @@ | |
.tags-selector { | ||
margin-top: 20px; | ||
} | ||
|
||
.admin-dropbox { | ||
width: 100%; | ||
margin-bottom: 20px; | ||
} | ||
} | ||
|
||
|
||
|