js下载图片


TOC

js下载图片

使用超链接下载图片的时候,会变成预览,使用下面的方法,就可以直接下载图片了

      downUrl(url) {
        // window.open(_this.detail.imgUrl)
        let xmlhttp = new XMLHttpRequest();
        xmlhttp.open("GET", url, true);
        xmlhttp.responseType = "blob";
        xmlhttp.onload = function () {
          if (this.status == 200) {
            const blob = this.response;
            const link = document.createElement('a');
            link.href = window.URL.createObjectURL(blob);
            link.download = url.substring(url.lastIndexOf("/") + 1, url.length);


            //此写法兼容可火狐浏览器
            document.body.appendChild(link);


            const evt = document.createEvent("MouseEvents");
            evt.initEvent("click", false, false);
            link.dispatchEvent(evt);
            window.URL.revokeObjectURL(link.href)
            document.body.removeChild(link);
          }
          ;
        }
        xmlhttp.send();
      }





免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM