好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

主頁 > 知識(shí)庫 > 利用canvas實(shí)現(xiàn)圖片下載功能來實(shí)現(xiàn)瀏覽器兼容問題

利用canvas實(shí)現(xiàn)圖片下載功能來實(shí)現(xiàn)瀏覽器兼容問題

熱門標(biāo)簽:標(biāo)準(zhǔn)智能外呼系統(tǒng) 搜狗星級(jí)酒店地圖標(biāo)注 洛陽市伊川縣地圖標(biāo)注中心官網(wǎng) 電銷機(jī)器人視頻 江蘇高頻外呼系統(tǒng)線路 高德地圖標(biāo)注錯(cuò)誤怎么修改 地圖標(biāo)注自己去過的地方 會(huì)聲會(huì)影怎樣做地圖標(biāo)注效果 平頂山電子地圖標(biāo)注怎么修改

前言:項(xiàng)目中需要實(shí)現(xiàn)圖片下載功能,第一個(gè)想到的是使用a標(biāo)簽的download屬性來實(shí)現(xiàn),但是在不同瀏覽器下測(cè)試會(huì)發(fā)現(xiàn),有的瀏覽器無效,點(diǎn)擊后直接預(yù)覽圖片,所以,上網(wǎng)找到了另外一種兼容不同瀏覽器的圖片下載的方法,那就是利用canvas來處理圖片,實(shí)現(xiàn)下載;

1.項(xiàng)目中點(diǎn)擊事件綁定:

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

2.點(diǎn)擊事件中操作:

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把圖片轉(zhuǎn)成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'是前端對(duì)圖片的跨域處理;

4.this.convertBase64UrlToBlob(base64)是將圖片base64流文件轉(zhuǎn)成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系統(tǒng)有安全性限制,以上方法在ios上無效;

以上就是記錄項(xiàng)目中用到的圖片下載,瀏覽器兼容的問題,涉及到的base64和blob的知識(shí)點(diǎn)和原理還不是很清晰,有時(shí)間一定要研究一下,整個(gè)方法,親測(cè)有效;歡迎測(cè)用,與意見反饋。也希望大家多多支持腳本之家。

標(biāo)簽:松原 果洛 阿克蘇 常德 蚌埠 廣西 廣東 鄂爾多斯

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《利用canvas實(shí)現(xiàn)圖片下載功能來實(shí)現(xiàn)瀏覽器兼容問題》,本文關(guān)鍵詞  利用,canvas,實(shí)現(xiàn),圖片下載,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《利用canvas實(shí)現(xiàn)圖片下載功能來實(shí)現(xiàn)瀏覽器兼容問題》相關(guān)的同類信息!
  • 本頁收集關(guān)于利用canvas實(shí)現(xiàn)圖片下載功能來實(shí)現(xiàn)瀏覽器兼容問題的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章