利用canvas实现图片下载功能来实现浏览器兼容问题

7次阅读

共计 2263 个字符,预计需要花费 6 分钟才能阅读完成。

前言: 项目中需要实现图片下载功能,第一个想到的是使用 a 标签的 download 属性来实现,但是在不同浏览器下测试会发现,有的浏览器无效,点击后直接预览图片,所以,上网找到了另外一种兼容不同浏览器的图片下载的方法,那就是利用 canvas 来处理图片,实现下载;

1. 项目中点击事件绑定:

<a href="#" @click.prevent="downloadIamge(imgsrc, name)"><span>{{name}}</span></a>

2. 点击事件中操作:

downloadIamge (imgsrc, name) {
      const url = imgsrc
      this.convertUrlToBase64(url).then((base64) => {const blob = this.convertBase64UrlToBlob(base64)
        if (getBrowser() === 'IE' || getBrowser() === 'Edge') {window.navigator.msSaveBlob(blob, name)
        } else {const a = document.createElement('a')
          const body = document.querySelector('body')
          a.download = name || 'image'
          a.href = URL.createObjectURL(blob)
          a.style.display = 'none'
          body.appendChild(a)
          a.click()
          body.removeChild(a)
          window.URL.revokeObjectURL(a.href)
        }
      })
    },

分析:
3.this.convertUrlToBase64(url) 就是利用 canvas 和 toDataURL 把图片转成 base64 格式并返回

convertUrlToBase64 (url) {return new Promise((resolve, reject) => {const img = new Image()
        img.crossOrigin = 'Anonymous'
        img.src = url
        img.onload = function () {const canvas = document.createElement('canvas')
          canvas.width = img.width
          canvas.height = img.height
          const ctx = canvas.getContext('2d')
          ctx.drawImage(img, 0, 0, img.width, img.height)
          const ext = img.src.substring(img.src.lastIndexOf('.') + 1).toLowerCase()
          const dataURL = canvas.toDataURL('image/' + ext)
          const base64 = {
            dataURL: dataURL,
            type: 'image/' + ext,
            ext: ext
          }
          resolve(base64)
        }
      })
    },

其中:img.crossOrigin = ‘Anonymous’ 是前端对图片的跨域处理;


4.this.convertBase64UrlToBlob(base64) 是将图片 base64 流文件转成 blob 文件

convertBase64UrlToBlob (base64) {const parts = base64.dataURL.split('base64,')
      const contentType = parts[0].split(':')[1]
      const raw = window.atob(parts[1])
      const rawLength = raw.length
      const uInt8Array = new Uint8Array(rawLength)
      for (let i = 0; i < rawLength; i++) {uInt8Array[i] = raw.charCodeAt(i)
      }
      return new Blob([uInt8Array], {type: contentType})
    },

5.getBrowser() 用来判断浏览器,解决浏览器兼容性问题:

import {getBrowser} from '@/utils/utils'
export function getBrowser () {
  const userAgent = navigator.userAgent
  if (userAgent.indexOf('OPR') > -1) {return 'Opera'}
  if (userAgent.indexOf('Firefox') > -1) {return 'FF'}
  if (userAgent.indexOf('Trident') > -1) {return 'IE'}
  if (userAgent.indexOf('Edge') > -1) {return 'Edge'}
  if (userAgent.indexOf('Chrome') > -1) {return 'Chrome'}
  if (userAgent.indexOf('Safari') > -1) {return 'Safari'}
}

6. 如果是 IE 或者 Edge 浏览器,可以直接使用 window.navigator.msSaveBlob(blob, name) 完成下载;


声明:由于 ios 系统是安全性限制,以上方法在 ios 上无效;

以上就是记录项目中用到的图片下载,浏览器兼容的问题,涉及到的 base64 和 blob 的知识点和原理还不是很清晰,有时间一定要研究一下,整个方法,亲测有效;欢迎测用,与意见反馈。

正文完
 0