DownloadImgZP = imgPath => { const image = new Image(); // 解決跨域 Canvas 污染問題 image.setAttribute('crossOrigin', 'anonymous'); image.onload = function() { const canvas = document.createElement('canvas'); canvas.width = image.width; canvas.height = image.height; const context = canvas.getContext('2d'); context.drawImage(image, 0, 0, image.width, image.height); const url = canvas.toDataURL('image/png'); // 獲得圖片的base64編碼數據 const a = document.createElement('a'); // 生成一個a元素 const event = new MouseEvent('click'); // 建立一個單擊事件 a.download = 'img.png' || 'photo'; // 設置圖片名稱 a.href = url; // 將生成的URL設置爲a.href屬性 a.dispatchEvent(event); // 觸發a的單擊事件 }; image.src = imgPath; };