将javascript数据下载到IE8中的文件

时间:2013-12-10 14:06:32

标签: javascript html internet-explorer

我有代码将javascript变量下载为文件,但它在IE8中不起作用,我将如何在IE8中执行此操作?

function download(filename, text) {
    var pom = document.createElement('a');
    pom.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
    pom.setAttribute('download', filename);

    document.body.appendChild(pom)
    pom.click();

    // Remove anchor from body
    document.body.removeChild(pom)
}

1 个答案:

答案 0 :(得分:1)

在IE 8和9中,数据URI只能用于图像,但不能用于导航或Javascript生成的文件下载。

请参阅http://en.wikipedia.org/wiki/Data_URI_scheme#Disadvantages

您可以通过服务器端处理文件来实现下载。