这里的img指一个<img>标签或者一个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;
至此 一个下载 就完成了;
URL.createObjectURL 可实现文本类下载;下回分解;