Downloading a Dynamic CSV in Internet Explorer

后端 未结 2 1267
迷失自我
迷失自我 2020-12-15 07:29

The following code works in both FireFox and Chrome, but not IE. Essentially, I have a JSON object which gets converted into an array and then to a csv format, when I click

2条回答
  •  渐次进展
    2020-12-15 08:09

    Internet Explorer does not permit data URIs as navigable content, for security purposes. To understand why, I would encourage you to read Henning Klevjer's short white-paper on the topic, Phishing by data URI. In summary, this has been demonstrated to open up avenues by which the end-user could be tricked into giving up sensitive information.

    Also, from the data Protocol documentation on MSDN:

    For security reasons, data URIs are restricted to downloaded resources. Data URIs cannot be used for navigation, for scripting, or to populate frame or iframe elements.

    To be honest, passing a data URI to window.open feels a bit hacky. Instead, you should use an API to handle the process (provided one exists). If you'd like to download a file to the user's machine in Internet Explorer, consider using navigator.msSaveBlob or navigator.msSaveOrOpenBlob.

    As an example, consider the following:

    if ( window.navigator.msSaveOrOpenBlob && window.Blob ) {
        var blob = new Blob( [ "A,B\nC,D" ], { type: "text/csv" } );
        navigator.msSaveOrOpenBlob( blob, "strings.csv" );
    }
    

提交回复
热议问题