简体   繁体   中英

Download tag and blob don't work for chrome and IE?

I want users to be able to download a file containing some data with JavaScript, which worked about a month ago, but suddenly started giving me issues both on chrome and IE.

Chrome downloads my file with the name "download" without an extension, ignoring the file name I give it. When I open the file with notepad++, the data is actually there, so it's only the name and file extension which is bugging. Here's my code for chrome:

var uri = 'data:text/csv;charset=utf-8,' + escape(text);
        var link = document.createElement("a");
        link.href = uri;
        link.style = "visibility:hidden";
        link.download = filename + ".csv";
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);

On IE I get an "InvalidStateError" when I try run the following code:

var blob = new Blob([text],{
            type: "text/csv;charset=utf-8;"
        });
        navigator.msSaveBlob(blob, filename + ".csv")

I found this solution online for IE as I couldn't use the download attribute for IE, which DID work a while ago, but doesn't now for some reason.

The code running for chrome is tested on firefox as well and starts a download with th ecorrect filename and extension.

Any help will be greatly appretiated!

I read some weeks ago the new version of Google Chrome have a bug with the download attribute.

Here you can see the compatibility of the download with all browsers and versions can I use . The IE browsers don't have support for it.

At the moment I can't find a solution for the download attribute, if someone got it that will be great!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM