jtopo 实现调用saveImageInfo()方法直接下载图片

首先找到源代码里的saveImageInfo 方法

                this.saveImageInfo = function (a, b) {
                    var c = this.eagleEye.getImage(a, b),
                        d = window.open("about:blank");
                    return d.document.write("from canvas"),
                        this
                },

把以下代码复制替换掉上面代码:

                this.saveImageInfo = function (a, b) {
                    var c = this.eagleEye.getImage(a, b);
                    function dataURLtoBlob(dataurl) {
                        var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
                            bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
                        while (n--) {
                            u8arr[n] = bstr.charCodeAt(n);
                        }
                        return new Blob([u8arr], { type: mime });
                    }
                    var blob_ = dataURLtoBlob(c); // 用到Blob是因为图片文件过大时,在一部风浏览器上会下载失败,而Blob就不会
                    var url;
                    url = {
                        name: "图片名称", // 图片名称不需要加.png后缀名
                        src: blob_
                    };

                    if (window.navigator.msSaveOrOpenBlob) {   // if browser is IE
                        navigator.msSaveBlob(url.src, url.name+'.png');//filename文件名包括扩展名,下载路径为浏览器默认路径
                    } else {
                        var link = document.createElement("a");
                        link.setAttribute("href", window.URL.createObjectURL(url.src));
                        link.setAttribute("download", url.name+'.png');
                        document.body.appendChild(link);
                        link.click();
                    }
                    return  this
                },

小手一点下载按钮,下载成功^_^

你可能感兴趣的:(jtopo)