Jqplot image download by a button click in IE

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-11 05:44:50

问题


I have jqplot and I want to download it once click a button as a jpg or png. I can do it using

$('#chartdiv').jqplotSaveImage();

(chartdiv is the div with plot)

It is working in chrome and firefox only. In IE it is not working.I tried in IE 11.

And I have another problem in chrome the downloaded image file name is 'download' and in firefox it is some wired name with .part extension (ex :- ka8ShgKH.part). Is there a way to put plot title as the download file name ?

thank you.

$("#btnSaveImg").on("click", LoadImage);

LoadImage = function(){
    $('#chartdiv').jqplotSaveImage();
}

EDIT jqplotsaveimage function

$.fn.jqplotSaveImage = function() {
    var imgData = $(this).jqplotToImageStr({});
    if (imgData) {
        window.location.href = imgData.replace("image/png", "image/octet-stream");
    }

};

回答1:


I'm using jqPlot and I had issues using the above in IE and even if it worked in Chrome I could not name the downloaded image.

I found this case force download base64 image and the http://danml.com/download.html . That's working both in later versions of IE and Chrome and you can save the image with your own filename. The download.js can be used for more than just images.

//include the downoad.js file from http://danml.com/download.html

<button id="dwnl-chart-1" onClick="$.fn.jqplotSaveImage('chart-1', 'chart_name">Download as image</button>

    $.fn.jqplotSaveImage = function(id, filename) {

        var imgData = $('#'+id+'-parent').jqplotToImageStr({});
        if (imgData) {
            download(imgData, filename+'.png', "image/png");
        }
    };


来源:https://stackoverflow.com/questions/21905400/jqplot-image-download-by-a-button-click-in-ie

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!