1、流文件下载
effectExport() {
let query={};
this.$axios.post(
CONFIG.httphost()+'/exportExcel/exportExcel_valid',query,
{responseType: "blob"}
)
.then(response =>{
this.exportLoading = false;
const BLOB = response.data;
const fileReader = new FileReader();
fileReader.readAsDataURL(BLOB);
fileReader.onload = (event) => {
let a = document.createElement("a");
a.download = `有效时长统计报表.xlsx`;
a.href = event.target.result;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
};
})
},
2、el-upload自动上传
<a-spin :spinning="spinning" tip="上传中">
<div class="m-5">
<el-upload
ref="uploads"
action="/"
accept='.xls,.xlsx'
drag
multiple
:http-request="uploadFile"
>
<i class="el-icon-upload"></i>
<div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
</el-upload>
</div>
</a-spin>
uploadFile( params ){
let formData = new FormData();
formData.append('id', this.itemId);
formData.append('tableid',this.tableid);
formData.append('file', params.file);
this.spinning = true;
this.$api.post(`/api/upload`, formData)
.then( res=>{
let code = res.code;
if( code == 0 ){
this.$refs.uploads.clearFiles();
this.successTit('成功', '文件挂接成功');
this.fileList = [];
}else{
this.failTit('失败', `文件挂接失败,错误码:${code}`)
}
}).catch(err=>{
thsi.failTit('失败', `文件挂接失败,错误码:${err}`)
})
.finally(() => {
this.spinning = false;
})
}
3、el-upload手动上传
<template>
<el-dialog title="导入Excel" :visible.sync="visible" @close='handleCancel' :close-on-click-modal='false' width="30%" >
<div class="body flex">
<el-upload
ref="upload"
accept='.xls,.xlsx'
:auto-upload="false"
:file-list="fileList"
:before-upload="beforeUpload"
:http-request="uploadHttpRequest"
drag
action="#"
:multiple='false'
:on-success="handleSuccess"
:on-error="handleError"
:on-remove="fileRemove"
:on-change="fileChange"
>
<i class="el-icon-upload"></i>
<div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
<div class="el-upload__tip" slot="tip">
一次只能上传一个xls/xlsx文件,且不超过10M
</div>
</el-upload>
</div>
<span slot="footer" class="dialog-footer">
<el-button @click="handleCancel">取 消</el-button>
<el-button type="primary" :disabled='uploadDisabled' @click="handleSubmit">确定上传</el-button>
</span>
</el-dialog>
</template>
<script>
export default {
data() {
return {
visible: false,
uploadType: 1,
fileList:[],
uploadDisabled:false,
tableid:0,
radioStyle: {
display: 'block',
height: '30px',
lineHeight: '30px',
},
};
},
methods: {
show(tableid) {
this.visible = true
this.tableid = tableid
},
beforeUpload(file) {
const isType = file.type === 'application/vnd.ms-excel'
const isTypeComputer = file.type === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
const fileType = isType || isTypeComputer
if(!fileType) {
this.$message.error('上传文件只能是xls/xlsx格式!')
}
const fileLimit = file.size / 1024 / 1024 < 10;
if(!fileLimit) {
this.$message.error('上传文件大小不超过10M!');
}
return fileType && fileLimit
},
handleSuccess(){
this.$message.success('文件上传成功');
},
handl
eError(){
this.$message.error('文件上传失败');
},
uploadHttpRequest(param) {
const formData = new FormData()
formData.append('filepath', param.file)
formData.append('tableid', this.tableid)
formData.append('isAutoDocNumber', this.uploadType);
this.$api.post('/api/filemake/Excelread',formData)
.then(res=>{
let code = res.code;
if(code == 0){
param.onSuccess();
}else{
this.failTit(res.errormsg || "条目导入失败")
}
})
.catch(err=>{
param.onError()
})
.finally(()=>{
this.$emit('ok')
})
},
fileChange(file, fileList) {
if (fileList.length > 0) {
this.fileList = [fileList[fileList.length - 1]]
}
},
fileRemove(file, fileList) {
if(fileList.length < 1) {
this.uploadDisabled = true
}else{
this.uploadDisabled = false
}
},
handleSubmit() {
this.visible = false
this.$refs.upload.submit()
this.$emit("upload")
},
handleCancel() {
this.visible = false;
this.$refs.upload.clearFiles()
this.fileList = []
},
onUploadChange( e ){
console.log('radio checked', e.target.value);
}
},
};
4、a-upload头像自动上传
<a-upload
name="file"
accept="image/*"
:showUploadList="false"
:multiple="false"
:customRequest="__upload_folder_request"
>
<div class="avatar mx-5">
<img class="rounded-full w-20 h-20" :src="avatar" alt="">
<img class="photo" :src="require('@/assets/profile/camera-green.png')" alt="">
</div>
</a-upload>
__upload_folder_request(data){
const formData = new FormData()
formData.append('avatar', data.file)
this.$api.post(`/userinfo/user/updateAvatar`, formData )
.then( res=>{
let code = res.code;
if( code == 0 ){
let avatar = res.data;
this.$store.dispatch("user/setUserAvatar", avatar)
this.avatar = avatar;
}
})
},