html代码段:
<input type="file" @change.prevent.stop="upload">
vue 中script代码段:
data() {
fileName: ''
},
methods: {
upload(event) {
let files = event.target.files[0];
this.fileName = this.getObjectUrl(files);
},
getObjectUrl(file) {
let url = null;
if (window.createObjectURL != undefined) {
// basic
url = window.createObjectURL(file);
} else if (window.webkitURL != undefined) {
// webkit or chrome
url = window.webkitURL.createObjectURL(file);
} else if (window.URL != undefined) {
// mozilla(firefox)
url = window.URL.createObjectURL(file);
}
return url;
}
}