其他分享
首页 > 其他分享> > 兼容IE图片下载

兼容IE图片下载

作者:互联网

function downloadImg(type,data,name){

  if(type == 1){

    var url = data

    //通过地址判断是否为图片类型文件

    var ext = url.slice(url.lastIndexOf('.')+1).toLowerCase()

    if(isImage(ext)){

      convertUrlTobase64(url).then(function(base64){

        var blob = convertBase64UrlToBlob(base64)

        if(myBrowser() == 'IE'){

          window.navigator.msSaveBlob(blob,name+'.jbg')

        }else{

          var a = document.createElement('a')

          a.download = name

          a.href = URL.createObjectURL(blob)

          a.dispatchEvent(new MouseEvent('click',{bubbles:true,view:window}))//兼容火狐浏览器

          document.body.removeChild('a')

        }

      })

    }else{

      var a = document.createElement('a')

      a.download = name

      a.href = url

      a.click()

      document.body.removeChild('a')

    }

  }else{

    var dataURL = data.toDataaURL('image/jpeg',1.0)

    var base64 = {

      dataURL:dataURL,

      type:'image.jpg',

      ext:'jpg'

    }

    var blob = convertBase64UrlToBlob(base64)

    if(myBrowser()=='IE'){

      window.navigator.msSaveBlob(blob,name+'.jbg')

    }else{

      var a = document.createElement('a')

      a.download = name

      a.href = url

      a.click()

      document.body.removeChild('a')

    }

  }

}

function convertBase64UrlToBlob(base64){//将base64转换为blob对象,blob存储2进制对象的容器

  var parts base64.dataURL.split(';base64,')

  var contentType = parts[0].split(':')[1]

  var raw = window.atob(parts[1])

  var rawLength = raw.length

  var uInt8Array = new Uint8Array(rawLength)

  for(var i=0;i<rawLength;i++){

    uInt8Array[i]=raw.charCodeAt(i)

  }

  return new Blob([uInt8Array],{type:contentType})

}

function convertUrlToBase64(url){

  return new Promise(funtion (resolve,reject){

   var img = new Image()

   img.crossOrigin = 'Anonymous';

   img.src = url

   img.onload= function(){

    var canvas = document.createElement('canvas')

    canvas.width = img.width

    canvas.height = img.height

    var ctx = canvas.getContent('2d')

    ctx.drawImage(img,0,0,img.width,img.height)

    var ext = img.src.substring(img.src.lastIndexOf('.')+1).toLowerCase()

    var dataURL = canvas.toDadaURL('image/'+ext)

    var base64={

      dataURL:dataURL,

      type:'image/'+ext,

      ext:ext

    }

    resolve(base64)

   }

  })

}

function myBrower(){//判断浏览器类型

  var userAgent = navigator.userAgent;//取得浏览器的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"

  }

}

function isImage(ext){

  if(ext == 'png' || ext == 'jpg' ||  ext == 'jpg' || ext == 'jpeg' || ext == 'gif' || ext == 'bmp' ){

    return true

  }

}

标签:return,img,base64,图片下载,兼容,ext,var,userAgent,IE
来源: https://www.cnblogs.com/riverslake/p/15766124.html