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 //文件類(lèi)型 var fileName = file.name.substring(file.name.lastIndexOf('.') + 1) if (fileName != 'xlsx' && fileName != 'xls') { this.$message.error('文件類(lèi)型必須是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)文件類(lèi)型是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é)
寫(xiě)在最后:
上述代碼均是自己在開(kāi)發(fā)過(guò)程中總結(jié)出來(lái),或許還有不足之處,但是希望有些內(nèi)容能夠幫到大家,也希望大家多多支持腳本之家。
相關(guān)文章
vue+vant使用圖片預(yù)覽功能ImagePreview的問(wèn)題解決
這篇文章主要介紹了vue+vant使用圖片預(yù)覽功能ImagePreview的問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04Vue v2.5 調(diào)整和更新不完全問(wèn)題
這篇文章主要介紹了Vue v2.5 調(diào)整和更新不完全問(wèn)題的相關(guān)資料,需要的朋友可以參考下2017-10-10Vue動(dòng)態(tài)面包屑功能的實(shí)現(xiàn)方法
面包屑功能是我們?cè)陧?xiàng)目中經(jīng)常遇到的功能,今天小編使用Element-UI 進(jìn)行實(shí)現(xiàn)在vue項(xiàng)目中實(shí)現(xiàn)面包屑功能,具體實(shí)現(xiàn)方式大家跟隨小編一起學(xué)習(xí)吧2019-07-07vue實(shí)現(xiàn)大文件切片斷點(diǎn)續(xù)傳
上傳文件,基本上用了input框就可以解決,但大文件應(yīng)該怎樣上傳呢,一次性上傳明顯不現(xiàn)實(shí),因?yàn)槊看我粩嗑W(wǎng),那就會(huì)從頭開(kāi)始上傳,所以切片勢(shì)在必行,關(guān)鍵就是如何切,怎么保障文件數(shù)據(jù)不會(huì)丟失,同時(shí)優(yōu)化上傳保障機(jī)制,本文就來(lái)給大家講講如何上傳大文件2023-07-07詳解使用VUE搭建后臺(tái)管理系統(tǒng)(vue-cli更新至3.0)
這篇文章主要介紹了詳解使用VUE搭建后臺(tái)管理系統(tǒng)(vue-cli更新至3.0),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-08-08vue相同路由跳轉(zhuǎn)強(qiáng)制刷新該路由組件操作
這篇文章主要介紹了vue相同路由跳轉(zhuǎn)強(qiáng)制刷新該路由組件操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08vue頁(yè)面切換項(xiàng)目實(shí)現(xiàn)轉(zhuǎn)場(chǎng)動(dòng)畫(huà)的方法
這篇文章主要介紹了vue頁(yè)面切換項(xiàng)目實(shí)現(xiàn)轉(zhuǎn)場(chǎng)動(dòng)畫(huà)的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11淺談VUE監(jiān)聽(tīng)窗口變化事件的問(wèn)題
下面小編就為大家分享一篇淺談VUE監(jiān)聽(tīng)窗口變化事件的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-02-02