這里的img指一個(gè)<img>標(biāo)簽或者一個(gè)src鏈接溶耘;
/**
* 只展示原理 不講究封裝
*/
var src = getSrc(); // img的src或者其他方式獲取
var tempImg = document.createElement('img');
var tempLink = document.createElement('img');
img.onload = function() {
var tempCanvas = document.createElement('canvas');
var oTx = tempCanvasgetContext('2d');
tempCanvas.width = tempImg.naturalWidth || exportImage.width;
tempCanvas.height = tempImg.naturalHeight || exportImage.height;
context.drawImage(exportImage, 0, 0, canvas.width, canvas.height);
if (window.navigator.msSaveBlob) {
window.navigator.msSaveBlob(canvas.msToBlob(), 'test.png');
} else if ('link' in tempLink) {
tempLink.setAttribute('src', canvas.toDataURL());
tempLink.setAttribute('download', 'test.png');
tempLink.click();
} else {
window.open( canvas.toDataURL()); // 在新窗口打開圖片
}
tempImg = null; tempLink=null; tempCanvas=null;
};
img.src = src;
至此 一個(gè)下載 就完成了;
URL.createObjectURL 可實(shí)現(xiàn)文本類下載凳兵;下回分解;