url上传图标转base64

methods:{

 // 发票验证-OCR发票查验 由于跨域,只能传网上预览
    getInvoiceOcr() {
      this.InvoiceLoading = true
      const base1 = this.baseurl + ':9527/mystatic/ce/capitalexpend/' + this.detailDialog.fileList[0].url
      this.strimagebase64 = ''
      var that = this
      var image = new Image()
      image.src = base1 + '?v=' + Math.random() // 处理缓存
      image.crossOrigin = '*' // 支持跨域图片
      image.onload = function() {
        var base64 = that.drawBase64Image(image)
        that.strimagebase64 = base64.split(',')[1]
        // 转完base64才能走接口
        if (that.strimagebase64) {
          invoiceOCR({
            'lngproductid': 315,
            'lngreceipttypeid': 2,
            'strqrcode': '',
            'strfilepath': '/mystatic/ce/capitalexpend/' + that.detailDialog.fileList[0].url, // 后台返回图片路径拼接 1234.png
            'strfilerealname': that.detailDialog.fileList[0].name, // 上传图片的名称 通行费.png
            'imgurl': that.strimagebase64 // 'BASE64'
          }).then(res => {
            that.InvoiceLoading = false
            if (res.code === 20000) {
              that.$message.success(res.message)
              that.detailDialog.fileList[0].intinvoicestatus = 0
              that.detailDialog.fileList[0].strinvoicemsg = res.message
            } else {
              that.detailDialog.fileList[0].intinvoicestatus = 1
              that.detailDialog.fileList[0].strinvoicemsg = res.message
            }
          })
        }
      }
    },
    // this.getBase64Image('http://wwww.test/test.png')
    // 参数是图片的URL地址
    // getBase64Image(url) {
    //   this.strimagebase64 = ''
    //   var that = this
    //   var image = new Image()
    //   image.src = url + '?v=' + Math.random() // 处理缓存
    //   image.crossOrigin = '*' // 支持跨域图片
    //   image.onload = function() {
    //     var base64 = that.drawBase64Image(image)
    //     that.strimagebase64 = base64.split(',')[1]
    //   }
    // },
    drawBase64Image(img) {
      var canvas = document.createElement('canvas')
      canvas.width = img.width
      canvas.height = img.height
      var ctx = canvas.getContext('2d')
      ctx.drawImage(img, 0, 0, img.width, img.height)
      var dataURL = canvas.toDataURL('image/png')
      return dataURL
    }
}

猜你喜欢

转载自www.cnblogs.com/hellofangfang/p/13396863.html