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()
      }
    })
  • 相关阅读:
    二维码生成插件(jquery.qrcode.js)说明文档
    JS&PHP如何实现二维码的生成以及识别(代码)
    【干货】Chrome插件(扩展)开发全攻略 写在前面
    电脑连接并调试手机浏览器的网页
    php操作mysql数据库(增删改查)
    springBoot+springCloud学习笔记
    HttpClient远程调用接口
    fastjson List<> 转Json , Json 转List<>
    连接redis失败,关闭防火墙即可
    复习mybatis框架(一)----映射文件
  • 原文地址:https://www.cnblogs.com/wilsunson/p/14169279.html
Copyright © 2011-2022 走看看