1. 程式人生 > 實用技巧 >elementui轉base64上傳檔案

elementui轉base64上傳檔案

<el-upload
  ref='upload'
  :auto-upload='false'
  :file-list="fileList"
  :multiple='false'
  :limit="1"
  :on-exceed="handleExceed"
  :http-request="uploadFiles"
  accept="image/jpeg,image/gif,image/png"
  action=''
  :on-change='changeUpload'     
  >
 <el-button slot="trigger" size="mini" type="primary">選取圖片</el-button>
 <span>&nbsp;</span>
 <el-button @click='uploadFiles' size="mini" type="primary">點選上傳</el-button>
</el-upload>
//點選上傳圖片,上傳成功返回圖片路徑 
uploadFiles(){
  var That=this;
 let file=this.$refs.upload.$refs['upload-inner'].$refs.input; //獲取檔案資料
 let fileList=file.files;   
 var imgFile;
 let reader = new FileReader();   //html5讀檔案
 reader.readAsDataURL(fileList[0]); //轉BASE64    
 reader.onload=function(e) {    //讀取完畢後呼叫介面
  imgFile = e.target.result;
  let obj
={ id: "loginLogo", configGroup: "logo", configItem : "loginLogo", itemValue : imgFile } return BaseApi.uploadFiles(obj).then((res)=>{ if(res.status=='SUCCESS'){ AlertBox('圖片上傳成功!','success',true).then(()=>{ return That.getSysLogo(); //呼叫獲取base64資料介面 }); }
else{ Alert('圖片上傳失敗',res); return '' } }) }; },

內容來自https://www.jb51.net/article/144933.htm