function down(){
var imgUrl = document.getElementById("img").src;
var xmlhttp;
xmlhttp = new XMLHttpRequest(); //用于在后臺與服務器交換數據
xmlhttp.open("GET", imgUrl, true); //語法 open(method,url,async)
xmlhttp.responseType = "blob"; // 請求返回的數據類型
xmlhttp.onload = function() { //處理返回的數據
if (this.status == 200) {
var blob = this.response;
var a = document.createElement('a');
a.href = window.URL.createObjectURL(blob); //圖片地址
a.download = 'down.jpg'; //下載時 圖片命名 如果沒有默認是下載的圖片的路徑
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
}
};
xmlhttp.send(); //用于發送http請求
}