最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Ant Design Upload 文件上傳功能的實現(xiàn)

 更新時間:2023年04月21日 15:57:19   作者:丶酸酸  
這篇文章主要介紹了Ant Design Upload 文件上傳功能的實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

一、Ant Design Vue文件上傳功能

1.文件上傳選項框

 <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ù)據導入</a-button>
    </div>

效果:

在這里插入圖片描述

在這里插入圖片描述

2.js實現(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: {
		   //點擊數(shù)據導入按鈕所調用的方法
		    showModal() {
		      this.visible = true
		    },
		    //對話框確認方法
		    handleOk(e) {
		      this.visible = false
		      this.confirmLoading = false
		    },
		    //關閉彈框
		    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
		    },
		    //顯示上傳文件內容
		    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ù)據
		      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('請求失敗! error:' + error)
		          this.spinning = false
		          that.visible = true
		        })
		      return false
		    }
	    }
    }
</script>

二、Ant Design React文件上傳功能

1.文件上傳選項框

render() {
        const upLoad = {
            name: 'files',
            action: 'http://192.168.0.102:7072/folder/annex/upload',//文件上傳地址
            headers: {
                authorization: 'authorization-text',
            },
            onChange: this.handleChange.bind(this),//上傳文件改變時的狀態(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)} //點擊方法
	                            className={styles.uploadBtn} //樣式
	                            type="primary"
	                            ghost
                             	>上傳</Button>
                        </div>
                        
						<Modal
                            title="文件上傳"
                            visible={this.state.uploadState}//是否顯示
                            onOk={this.handleOk.bind(this)}//確認方法
                            onCancel={this.handleCancel.bind(this)}//取消方法
                        >
                            <Dragger {...upLoad}>
                                <p className="ant-upload-drag-icon">
                                    <InboxOutlined />
                                </p>
                                <p className="ant-upload-text">單擊或拖動文件到此區(qū)域以上傳</p>
                            </Dragger>,
                		</Modal>
                		
                </React.Fragment>)
      }

效果:

在這里插入圖片描述

在這里插入圖片描述

2. js實現(xià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') {//當文件類型是xlsx,docx,pdf三種格式時
               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ù)據
               fd.append("id", response.id);//添加id數(shù)據
               fd.append("name", response.name);//添加name數(shù)據
               fd.append("suffix", response.suffix);//添加suffix數(shù)據
               fd.append("type", response.type);//添加type數(shù)據
               axios({
                   url: baseUrl + api,//文件數(shù)據保存的地址
                   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} 文件上傳失?。);
           }

       }
   }
//點擊確定按鈕方法
   handleOk = e => {
       this.setState({
           uploadState: false,
       });
   };
//取消方法
   handleCancel = e => {
       this.setState({
           uploadState: false,
       });
   };

總結

寫在最后:

上述代碼均是自己在開發(fā)過程中總結出來,或許還有不足之處,但是希望有些內容能夠幫到大家,也希望大家多多支持腳本之家。

相關文章

最新評論

樟树市| 双桥区| 边坝县| 旬邑县| 铜鼓县| 哈密市| 朔州市| 明水县| 呼图壁县| 临颍县| 湘潭市| 龙川县| 丹巴县| 平乐县| 舞阳县| 邢台县| 疏附县| 玉林市| 桦川县| 青海省| 寻甸| 汉源县| 六盘水市| 察隅县| 全椒县| 藁城市| 岳阳市| 孝义市| 绥宁县| 门源| 新兴县| 固安县| 贞丰县| 沐川县| 铅山县| 兴海县| 牡丹江市| 象州县| 阿克| 凌云县| 若羌县|