1.后端传过来的数据不是自己想要,然后如何去格式化?
<template>
<a-table
:columns="columns"
:data-source="data"
size="middle"
:row-key="record => record.id"
:pagination="paginationOption"
:loading="loading"
@change="change"
>
<template slot="handle" slot-scope="text, record">
<a href="javascript:;" @click="see(record)">预览文件</a>
<a href="javascript:;" @click="del(record)">删除文件</a>
</template>
</a-table>
</template>
//格式化时间
function formatterTime(time){
// 2020-07-17T17:02:52
let t1 = time.slice(0,10)
let t2 = time.slice(11)
return t1 + ' ' + t2
}
//格式化文件大小
function formatterFileSize(size){
// 1078729
if(size / 1024 / 1024 > 1){
return (size / 1024 / 1024).toFixed(2) + 'M'
}else{
if(size / 1024 > 1) {
return (size / 1024).toFixed(2) + 'K'
}else{
return size.toFixed(2) + 'B'
}
}
}
const columns = [
{
title: "文件名称",
dataIndex: "name"
// key: 'name',
},
{
title: "文件大小",
dataIndex: "maxSize",
// key: 'maxSize',
customRender: (text, row, index) => {
return formatterFileSize(text)
}
},
{
title: "上传日期",
dataIndex: "updTime",
// key: 'updTime',
customRender: (text, row, index) => {
return formatterTime(text)
}
},
{
title: "操作",
dataIndex: "handle",
// key: 'handle',
align: "center",
scopedSlots: { customRender: "handle" }
}
];
2.upload上传文件出了一点问题
<template>
<a-upload
name="file"
:showUploadList="false"
:customRequest="customRequest"
accept=".jpg, .jpeg, .png, .bmp, .tif, .gif, .PDF, .doc, .docx"
>
<div class="btn">
<a-icon type="plus" />上传文件
</div>
</a-upload>
</template>
<script>
export default {
data() {
return {
thisId: 0,
}
},
methods: {
//自定义上传文件
customRequest(info) {
const formData = new FormData();
formData.append("bizId", this.thisId);
formData.append("file", info.file);
this.saveFile(formData,info);
},
//上传文件
saveFile(formData,info) {
//addAttachment 为上传的接口
addAttachment(formData).then(res => {
this.$message.success(`${info.file.name} 上传成功`);
}).catch(err => {
console.error(err);
this.$message.error(`${info.file.name} 上传失败`);
})
},
}
}
</script>