mirror of
https://github.com/youzan/vant.git
synced 2025-10-20 18:54:24 +00:00
[new feature] Uploader: support multiple files (#480)
This commit is contained in:
@@ -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 = '');
|
||||
}
|
||||
}
|
||||
});
|
||||
|
Reference in New Issue
Block a user