element 上傳圖片轉base64


<el-upload
          list-type="picture-card"
          action=""
          :http-request="uploadFileFn"
          :show-file-list="false"
          :accept="mime"
          :on-change="getFile"
        >
        上傳
        </el-upload>
 
methods:{
// 圖片轉base64    
getFile(file, fileList) {
      this.getBase64(file.raw).then(res => {
        const params = res.split(',')
        console.log(params, 'params')
        if (params.length > 0) {
          this.strimagebase64 = params[1]
        }
      })
    },
    // 獲取圖片轉base64
    getBase64(file) {
      return new Promise(function(resolve, reject) {
        const reader = new FileReader()
        let imgResult = ''
        reader.readAsDataURL(file)
        reader.onload = function() {
          imgResult = reader.result
        }
        reader.onerror = function(error) {
          reject(error)
        }
        reader.onloadend = function() {
          resolve(imgResult)
        }
      })
    }
}
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM