IE 10: SCRIPT5: Access is Denied error on anchor click event

后端 未结 2 1262
深忆病人
深忆病人 2020-12-21 17:37

I\'m trying to save a SVG from a canvas as PNG file using javascript. The below code seems to work fine on Chrome and Firefox, but in IE 10 i get the below error in my conso

2条回答
  •  南笙
    南笙 (楼主)
    2020-12-21 18:16

    Necromancing.
    On IE, you don't need to create a link.
    It's sufficient to create a new Blob.

    function saveMe(data, fileName)
    {
    
        var json = JSON.stringify(data),
            blob = new Blob([json], { type: "octet/stream" }),
            url = window.URL.createObjectURL(blob);
    
        if (navigator.msSaveOrOpenBlob) 
        {
            navigator.msSaveOrOpenBlob(blob, fileName);
            return;
        }
        else if (window.navigator.msSaveBlob)
        { // for IE browser
            window.navigator.msSaveBlob(blob, fileName);
            return;
        }
    
        var a = document.createElement("a");
        document.body.appendChild(a);
        a.style = "display: none";
    
        a.href = url;
        a.download = fileName;
        a.click();
        window.URL.revokeObjectURL(url);
    }
    
    
    var data = { x: 42, s: "hello, world", d: new Date() }, fileName = "my-download.json";
    
    // saveData(data, fileName);
    saveMe(data, fileName);
    

提交回复
热议问题