zoukankan      html  css  js  c++  java
  • 网络图片转base64

    let xhr = new XMLHttpRequest()
        xhr.open('get', imgUrl='www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png', true)
        xhr.responseType = 'blob'
        xhr.onload = function () {
          if (this.status === 200) {
            let blob = this.response
            let fileReader = new FileReader()
            fileReader.onloadend = function (e) {
              // el.src = e.target.result
            }
            fileReader.readAsDataURL(blob)
          }
        }
      xhr.send()

    解决vue项目绑定img src但 ie不显示 于是写成自定义指令

    Vue.directive('toBase64', {
      bind: function (el, binding) {
        const { value: imgIds } = binding
        let showImg = ''
        let defaultImg = ''
        if (Array.isArray(imgIds)) {
          showImg = imgIds[0]
          defaultImg = imgIds[1]
        } else {
          showImg = imgIds
        }
        let imgUrl = config.imgBaseUrl + showImg
        if (!showImg || !window.navigator.msSaveBlob) {
          el.src = showImg ? imgUrl : defaultImg
          return
        }
        let xhr = new XMLHttpRequest()
        xhr.open('get', imgUrl, true)
        xhr.responseType = 'blob'
        xhr.onload = function () {
          if (this.status === 200) {
            let blob = this.response
            let fileReader = new FileReader()
            fileReader.onloadend = function (e) {
              el.src = e.target.result
            }
            fileReader.readAsDataURL(blob)
          }
        }
        xhr.send()
      },
      update: function (el, binding) {
        const { oldValue, value: imgIds } = binding
        if (oldValue.toString() === imgIds.toString()) {
          return
        }
        let showImg = ''
        let defaultImg = ''
        if (Array.isArray(imgIds)) {
          showImg = imgIds[0]
          defaultImg = imgIds[1]
        } else {
          showImg = imgIds
        }
        let imgUrl = config.imgBaseUrl + showImg
        if (!showImg || !window.navigator.msSaveBlob) {
          el.src = showImg ? imgUrl : defaultImg
          return
        }
        let xhr = new XMLHttpRequest()
        xhr.open('get', imgUrl, true)
        xhr.responseType = 'blob'
        xhr.onload = function () {
          if (this.status === 200) {
            let blob = this.response
            let fileReader = new FileReader()
            fileReader.onloadend = function (e) {
              el.src = e.target.result
            }
            fileReader.readAsDataURL(blob)
          }
        }
        xhr.send()
      }
    })
  • 相关阅读:
    SpringBoot之采用AOP统一打印日志信息
    SpringBoot之SpringBoot整合log4j
    SpringBoot之SpringBoot整合logback
    SpringBoot之SpringBoot整合多环境不同配置文件
    SpringBoot之SpringBoot整合devtools热部署
    [bug] Flask:jinja2.exceptions.UndefinedError: 'None' has no attribute 'id'
    [bug] Python:“TabError: inconsistent use of tabs and spaces in indentation”
    [bug] MySQL 无法删除表
    [bug] Flask css 不更新
    [bug] flink on yarn 启动失败
  • 原文地址:https://www.cnblogs.com/wilsunson/p/14169279.html
Copyright © 2011-2022 走看看