Upload: filter accepted file types in dragger (#10278)

pull/10279/head
杨奕 2018-03-21 11:56:30 +08:00 committed by GitHub
parent 3934e8f3ba
commit 3ede818e41
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 4 deletions

View File

@ -19,8 +19,10 @@ export default {
IframeUpload IframeUpload
}, },
provide: { provide() {
uploader: this return {
uploader: this
};
}, },
inject: { inject: {

View File

@ -17,6 +17,11 @@
props: { props: {
disabled: Boolean disabled: Boolean
}, },
inject: {
uploader: {
default: ''
}
},
data() { data() {
return { return {
dragover: false dragover: false
@ -29,10 +34,35 @@
} }
}, },
onDrop(e) { onDrop(e) {
if (!this.disabled) { if (this.disabled || !this.uploader) return;
this.dragover = false; const accept = this.uploader.accept;
this.dragover = false;
if (!accept) {
this.$emit('file', e.dataTransfer.files); this.$emit('file', e.dataTransfer.files);
return;
} }
this.$emit('file', [].slice.call(e.dataTransfer.files).filter(file => {
const { type, name } = file;
const extension = name.indexOf('.') > -1
? `.${ name.split('.').pop() }`
: '';
const baseType = type.replace(/\/.*$/, '');
return accept.split(',')
.map(type => type.trim())
.filter(type => type)
.some(acceptedType => {
if (/\..+$/.test(acceptedType)) {
return extension === acceptedType;
}
if (/\/\*$/.test(acceptedType)) {
return baseType === acceptedType.replace(/\/\*$/, '');
}
if (/^[^\/]+\/[^\/]+$/.test(acceptedType)) {
return type === acceptedType;
}
return false;
});
}));
} }
} }
}; };