利用js儲存圖片到本地
阿新 • • 發佈:2019-01-24
js程式碼:
<script> function download() { <span style="white-space:pre"> </span>var canvas = document.createElement('canvas'); <span style="white-space:pre"> </span>var ctx=canvas.getContext("2d"); <span style="white-space:pre"> </span>var awindow=new Image(); <span style="white-space:pre"> </span>awindow.src="httP://";//這地方輸入要儲存的圖片地址; <span style="white-space:pre"> </span><span style="white-space:pre"> </span>canvas.width=awindow.width;//圖片的寬度 <span style="white-space:pre"> </span>canvas.height=awindow.height;//圖片的高度 <span style="white-space:pre"> </span>ctx.drawImage(awindow,0,0);//畫布上新增圖片內容 <span style="white-space:pre"> </span>var dt = canvas.toDataURL('image/png');//儲存圖片格式,固定的格式 <span style="white-space:pre"> </span>this.href = dt; }; downloadLnk.addEventListener('click', download, false); </script>
Html程式碼:
<span style="font-size:14px;"><a id="downloadLnk" download="saveImage.jpg";>Save Image</a></span>