Ant Design Upload 文件上傳功能的實(shí)現(xiàn)
一、Ant Design Vue文件上傳功能
1.文件上傳選項(xiàng)框
<a-modal
title="上傳文件"
:footer="null"http://不顯示底部按鈕
:visible="visible"http://是否顯示
:confirmLoading="confirmLoading"http://確定按鈕 loading
@cancel="handleCancel"http://取消方法
>
<a-upload
:fileList="fileList"http://上傳的文件列表
:remove="handleRemove"http://文件刪除方法
:beforeUpload="beforeUpload"http://上傳文件之前的鉤子,參數(shù)為上傳的文件
>
<a-button>
<a-icon type="upload" />選擇文件
</a-button>
</a-upload>
</a-modal>
<div class="streetAdd">
<a-button type="primary" icon="plus" @click="engineeringadd()">新增</a-button>
<a-button type="primary" icon="file" @click="showModal()">數(shù)據(jù)導(dǎo)入</a-button>
</div>
效果:


2.js實(shí)現(xiàn)代碼
//定義的變量
<script>
export default {
data() {
return {
visible: false,
confirmLoading: false,
fileList: [],
IpConfig: this.IpConfig.projectServiceDomain,
}
},
mounted: function () {
this.engineeringList()
//that.alarmTypes=res.data.res.dictionaryList;
},
methods: {
//點(diǎn)擊數(shù)據(jù)導(dǎo)入按鈕所調(diào)用的方法
showModal() {
this.visible = true
},
//對(duì)話框確認(rèn)方法
handleOk(e) {
this.visible = false
this.confirmLoading = false
},
//關(guān)閉彈框
handleCancel(e) {
//console.log('Clicked cancel button')
this.visible = false
},
//刪除上傳文件
handleRemove(file) {
const index = this.fileList.indexOf(file)
const newFileList = this.fileList.slice()
newFileList.splice(index, 1)
this.fileList = newFileList
},
//顯示上傳文件內(nèi)容
beforeUpload(file) {
this.spinning = true
var that = this
that.visible = false
//文件類型
var fileName = file.name.substring(file.name.lastIndexOf('.') + 1)
if (fileName != 'xlsx' && fileName != 'xls') {
this.$message.error('文件類型必須是xls或xlsx!')
this.spinning = false
that.visible = true
return false
}
//讀取文件大小
var fileSize = file.size
//console.log(fileSize)
if (fileSize > 1048576) {
this.$message.error('文件大于1M!')
this.spinning = false
that.visible = true
return false
}
let fd = new FormData()//表單格式
fd.append('file', file)//添加file表單數(shù)據(jù)
let url = this.IpConfig + '/xing/jtGongLuShouFeiZhan/upload'
this.$ajax({
method: 'post',
url: url,
data: fd,
headers: {
'Content-Type':
'multipart/form-data;boundary=' + new Date().getTime(),
},
})
.then((rsp) => {
console.log(rsp)
let resp = rsp.data
if (rsp.status == 200) {
that.fileList = []
that.visible = false
that.confirmLoading = false
that.$message.success('文件上傳成功!')
this.spinning = false
} else {
this.$message.error('文件上傳失敗!')
that.visible = true
}
})
.catch((error) => {
this.$message.error('請(qǐng)求失敗! error:' + error)
this.spinning = false
that.visible = true
})
return false
}
}
}
</script>
二、Ant Design React文件上傳功能
1.文件上傳選項(xiàng)框
render() {
const upLoad = {
name: 'files',
action: 'http://192.168.0.102:7072/folder/annex/upload',//文件上傳地址
headers: {
authorization: 'authorization-text',
},
onChange: this.handleChange.bind(this),//上傳文件改變時(shí)的狀態(tài)
showUploadList: false,//是否展示文件列表
}
const { page, pages, fileType } = this.state;
return (<React.Fragment>
<div className={styles.tableBtnBox}>
<Button disabled={this.state.showBtn} type="primary">新建</Button>
<Button
disabled={this.state.showBtn} //是否可用
onClick={this.onUpload.bind(this)} //點(diǎn)擊方法
className={styles.uploadBtn} //樣式
type="primary"
ghost
>上傳</Button>
</div>
<Modal
title="文件上傳"
visible={this.state.uploadState}//是否顯示
onOk={this.handleOk.bind(this)}//確認(rèn)方法
onCancel={this.handleCancel.bind(this)}//取消方法
>
<Dragger {...upLoad}>
<p className="ant-upload-drag-icon">
<InboxOutlined />
</p>
<p className="ant-upload-text">單擊或拖動(dòng)文件到此區(qū)域以上傳</p>
</Dragger>,
</Modal>
</React.Fragment>)
}
效果:


2. js實(shí)現(xiàn)代碼
//點(diǎn)擊上傳按鈕方法
onUpload() {
this.setState({
uploadState: true,
})
}
//文件上傳
handleChange(info) {
var fileSize = info.file.size;
if (info.file.status === 'done') {
if (info.file.response.code === 500) {
message.error('上傳文件失敗');
return
}
let response = info.file.response.res[0];
if (response.suffix == 'xlsx' || response.suffix == 'docx' || response.suffix == 'pdf') {//當(dāng)文件類型是xlsx,docx,pdf三種格式時(shí)
let userid = Number(localStorage.getItem('userid'));
const baseUrl = 'http://192.168.0.123:8889';
const api = '/zhyjxxzhpt/file/upload';
let fd = new FormData();//表單格式
fd.append("userid", userid);//添加userid數(shù)據(jù)
fd.append("id", response.id);//添加id數(shù)據(jù)
fd.append("name", response.name);//添加name數(shù)據(jù)
fd.append("suffix", response.suffix);//添加suffix數(shù)據(jù)
fd.append("type", response.type);//添加type數(shù)據(jù)
axios({
url: baseUrl + api,//文件數(shù)據(jù)保存的地址
method: "post",
data: fd,
headers: {
"Content-Type": "multipart/form-data;boundary=" + new Date().getTime()
}
}).then(res => {
if (res.data.code == 'success') {
message.success(`${response.name} 文件上傳成功!`);
}
this.queryPrivate();
})
} else {
message.error(`只能上傳xlsx,docx,pdf文件!`);
return false
}
} else if (info.file.status === 'error') {
if (fileSize > 1048576) {
message.error(`${info.file.name}文件大于1M!`);
} else {
message.error(`${info.file.name} 文件上傳失敗!`);
}
}
}
//點(diǎn)擊確定按鈕方法
handleOk = e => {
this.setState({
uploadState: false,
});
};
//取消方法
handleCancel = e => {
this.setState({
uploadState: false,
});
};
總結(jié)
寫在最后:
上述代碼均是自己在開(kāi)發(fā)過(guò)程中總結(jié)出來(lái),或許還有不足之處,但是希望有些內(nèi)容能夠幫到大家,也希望大家多多支持腳本之家。
相關(guān)文章
使用D3.js+Vue實(shí)現(xiàn)一個(gè)簡(jiǎn)單的柱形圖
這篇文章主要介紹了使用D3.js+Vue實(shí)現(xiàn)一個(gè)簡(jiǎn)單的柱形圖,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-08-08
vue 實(shí)現(xiàn)邊輸入邊搜索功能的實(shí)例講解
今天小編就為大家分享一篇vue 實(shí)現(xiàn)邊輸入邊搜索功能的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
vue2.0 + element UI 中 el-table 數(shù)據(jù)導(dǎo)出Excel的方法
下面小編就為大家分享一篇vue2.0 + element UI 中 el-table 數(shù)據(jù)導(dǎo)出Excel的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
vue部署到線上為啥會(huì)出現(xiàn)404的原因分析及解決
這篇文章主要介紹了vue部署到線上為啥會(huì)出現(xiàn)404的原因分析及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-04-04
vue項(xiàng)目中圖片懶加載時(shí)出現(xiàn)的問(wèn)題及解決
這篇文章主要介紹了vue項(xiàng)目中圖片懶加載時(shí)出現(xiàn)的問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
vue 動(dòng)態(tài)組件用法示例小結(jié)
這篇文章主要介紹了vue 動(dòng)態(tài)組件用法,結(jié)合實(shí)例形式總結(jié)分析了vue 動(dòng)態(tài)組件基本功能、原理、使用方法及操作注意事項(xiàng),需要的朋友可以參考下2020-03-03
淺談vue項(xiàng)目可以從哪些方面進(jìn)行優(yōu)化
本篇文章主要介紹了淺談vue項(xiàng)目可以從哪些方面進(jìn)行優(yōu)化,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05

