使用图片地址进行下载

2021-07-27  本文已影响0人  燕自浩

解决方式1

export const downloadImg = (url: string, fileName: string) => {
  const x = new XMLHttpRequest()
  x.open('GET', url, true)
  x.responseType = 'blob'
  // eslint-disable-next-line func-names
  x.onload = function () {
    const resultUrl = window.URL.createObjectURL(x.response)
    const a = document.createElement('a')
    a.href = resultUrl
    a.download = fileName
    a.click()
  }
  x.send()
}

解决方式2


var downloadIamge = function(imgsrc, name) {//下载图片地址和图片名
  let image = new Image();
  // 解决跨域 Canvas 污染问题
  image.setAttribute("crossOrigin", "anonymous");
  image.onload = function() {
    let canvas = document.createElement("canvas");
    canvas.width = image.width;
    canvas.height = image.height;
    let context = canvas.getContext("2d");
    context.drawImage(image, 0, 0, image.width, image.height);
    let url = canvas.toDataURL("image/png"); //得到图片的base64编码数据
    let a = document.createElement("a"); // 生成一个a元素
    let event = new MouseEvent("click"); // 创建一个单击事件
    a.download = name || "photo"; // 设置图片名称
    a.href = url; // 将生成的URL设置为a.href属性
    a.dispatchEvent(event); // 触发a的单击事件
  };
  image.src = imgsrc;

上一篇下一篇

猜你喜欢

热点阅读