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