vue2项目中 加入 element-ui, axios
npm install element-ui -S --registry=https://registry.npm.taobao.org
npm install axios -S --registry=https://registry.npm.taobao.org
main.js 导入
import Element from 'element-ui'
import axios from 'axios'
新增模版
<template>
<el-upload
class="upload-demo"
action="#"
:http-request="handleChange"
:on-preview="handlePreview"
:on-remove="handleRemove"
:before-remove="beforeRemove"
:on-exceed="handleExceed"
:file-list="fileList">
<el-button size="small" type="primary">点击上传</el-button>
<div slot="tip" class="el-upload__tip">只能上传zip文件,且不超过100M</div>
</el-upload>
</template>
<script>
import axios from 'axios'
export default {
data() {
return {
fileList: []
};
},
methods: {
handleRemove(file, fileList) {
console.log(file, fileList);
},
handlePreview(file) {
console.log(file);
},
handleExceed(files, fileList) {
this.$message.warning(`当前限制选择 3 个文件,本次选择了 ${files.length} 个文件,共选择了 ${files.length + fileList.length} 个文件`);
},
beforeRemove(file, fileList) {
return this.$confirm(`确定移除 ${ file.name }?`);
},
handleChange(param) {
const data = new FormData();
data.append('file', param.file);
axios.post(process.env(自己的host配置) + '/upload_file', data, {
headers: {
'Content-Type': 'multipart/form-data',
},
}).then(res => {
console.log(res);
}).catch(err => {
console.log(err);
});
}
}
}
</script>
|