[new feature] Uploader: support multiple files (#480)

This commit is contained in:
neverland
2017-12-25 16:06:27 +08:00
committed by GitHub
parent 30f22b28ed
commit 4b0f4ac426
5 changed files with 84 additions and 66 deletions

View File

@@ -7,7 +7,7 @@
class="van-uploader__input"
v-bind="$attrs"
:disabled="disabled"
@change="onValueChange"
@change="onChange"
/>
</div>
</template>
@@ -29,30 +29,52 @@ export default create({
},
methods: {
onValueChange(event) {
if (this.disabled) {
onChange(event) {
let { files } = event.target;
if (this.disabled || !files.length) {
return;
}
const file = event.target.files[0];
if (!file || (this.beforeRead && !this.beforeRead(file))) {
files = files.length === 1 ? files[0] : [].slice.call(files, 0);
if (!files || (this.beforeRead && !this.beforeRead(files))) {
return;
}
const reader = new FileReader();
reader.onload = (e) => {
this.afterRead && this.afterRead({
file,
content: e.target.result
if (Array.isArray(files)) {
Promise.all(files.map(this.readFile)).then(contents => {
this.onAfterRead(
files.map((file, index) => ({
file: files[index],
content: contents[index]
}))
);
});
} else {
this.readFile(files).then(content => {
this.onAfterRead({ file: files, content });
});
this.$refs.input && (this.$refs.input.value = '');
};
if (this.resultType === 'dataUrl') {
reader.readAsDataURL(file);
} else if (this.resultType === 'text') {
reader.readAsText(file);
}
},
readFile(file) {
return new Promise(resolve => {
const reader = new FileReader();
reader.onload = event => {
resolve(event.target.result);
};
if (this.resultType === 'dataUrl') {
reader.readAsDataURL(file);
} else if (this.resultType === 'text') {
reader.readAsText(file);
}
});
},
onAfterRead(file) {
this.afterRead && this.afterRead(file);
this.$refs.input && (this.$refs.input.value = '');
}
}
});