// 文件上传
<el-form-item label="上传">
<el-upload
class="upload-demo"
ref="upload"
action="#"
:limit="1"
:http-request="uploadOk"
:on-preview="handlePreview"
:before-upload="beforeAvatarUpload"
:on-remove="handleRemove"
:file-list="fileList"
:auto-upload="true">
<el-button slot="trigger" size="small" type="primary">选取文件</el-button>
<div slot="tip" class="el-upload__tip">只能上传xls/xlsx文件</div>
</el-upload>
</el-form-item>
// 上传前验证
beforeAvatarUpload(file) {
let Xls = file.name.split('.');
if(Xls[1] === 'xls'||Xls[1] === 'xlsx'){
return file
}else {
this.$message.error('上传文件只能是 xls/xlsx 格式!')
return false
}
},
handleRemove(file, fileList) {
console.log(file, fileList);
},
handlePreview(file) {
console.log(file);
},
// 上传成功
uploadOk(val){
let fd = new FormData()
fd.append('upload_file',val.file)
fd.append('dir_name','contract')
// console.log(fd.upload_file) 错误
// console.log(fd.get("upload_file") 正确
this.$post('/upload/uploadFile', fd).then((result) => {
if (result.code == 200) {
this.$message({
message: '上传成功',
type: 'success'
});
// 接收excel文件地址
this.addForm.addurl = result.data.file_url
}else{
this.message(result.msg)
}
})
},
// 文件的下载
<template slot-scope="{scope}">
<span class="row-operate" @click="handleDownload(scope.row)">下载</span>
<template>
// 下载源文件
handleDownload: function(index, row){
if(row.contract_file_url == ''){ // row.contract_file_url 文件路径
this.$message({
message: '没有源文件',
type: 'warning'
})
}else{
// 创建a标签
let link = document.createElement('a')
// href链接
link.setAttribute('href', row.contract_file_url)
// 自执行点击事件
link.click()
}
},
// 请求接口,转化文件流
this.$get('/documentflow').then(res => {
let blob = new Blob([res], {type: "application/vnd.ms-excel"}); //res 就是文件流了
let objectUrl = URL.createObjectURL(blob);
window.location.href = objectUrl
})