<script src="//unpkg.com/vue@2/dist/vue.js"></script>
<script src="//unpkg.com/element-ui@2.15.14/lib/index.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<div id="app">
<el-upload
class="upload-demo"
:name="uploadId"
:ref="`Uploader-${uploadId}`"
action=""
list-type="picture-card"
:on-remove="handleRemove"
accept="image/*"
multiple
:file-list="fileList"
:on-change="handleChange"
:http-request="httpRequest"
:auto-upload="false"
>
<el-button slot="trigger" size="small" type="primary">选取文件
</el-button>
</el-upload>
</div>
var Main = {
data() {
return {
fileList: [],
uploadId: Math.random().toString(36).substr(2).toLocaleUpperCase(),
uploadFiles: [],
fm: new FormData(),
};
},
methods: {
handleRemove(file, fileList) {
console.log(file, fileList);
},
handleChange(file, fileList) {
(file.status == 'ready') && this.uploadFiles.push(file.raw);
this.fileTotal = document.getElementsByName(this.uploadId)[0].files.length;
if (this.uploadFiles.length === this.fileTotal) {
const Uploader = this.$refs[`Uploader-${this.uploadId}`];
Uploader.submit();
}
},
httpRequest(file) {
this.fm.append('file', file.file);
if (this.fm.getAll('file').length === this.fileTotal) {
this.$axios.post('https://jsonplaceholder.typicode.com/posts/', this.fm).then(res => {
this.$message.success('图片上传成功!')
}).catch(res => {
this.$message.success('图片上传失败!')
})
}
this.fileList = [];
}
}
}
Vue.prototype.$axios = axios
var Ctor = Vue.extend(Main)
new Ctor().$mount('#app')