search

Home  >  Q&A  >  body text

javascript - How to use a connection to create a dynamic download file stream in ie11?

The following function can download a txt file in other browsers, but jumps to a blank page in ie11. The file is URL-encoded and placed in the address bar. No download is triggered. How can I trigger downloading files in IE11?

Full project address: https://github.com/wangduandu...

    this.downloadLog = function() {
        var file = "data:text/plain;charset=utf-8,";
        var logFile = self.getLog();
        var encoded = encodeURIComponent(logFile);
        file += encoded;
        var a = document.createElement('a');
        a.href = file;
        a.target   = '_blank';
        a.download = self.formatTimestamp()+ '-' + self.logFilename;
        document.body.appendChild(a);
        a.click();
        a.remove();
    };

phpcn_u1582phpcn_u15822808 days ago597

reply all(1)I'll reply

  • PHPz

    PHPz2017-05-19 10:10:47

    After checking the information, you can use Microsoft’s exclusive msSaveBlob. This method supports ie10 and above.

    var downloadFileName = self.formatTimestamp()+ '-' + self.logFilename;
    
            if(window.navigator.msSaveBlob){
                // for ie 10 and later
                try{
                    var blobObject = new Blob([self.output]); 
                    window.navigator.msSaveBlob(blobObject, downloadFileName); 
                }
                catch(e){
                    console.log(e);
                }
            }
            else{
                var file = "data:text/plain;charset=utf-8,";
                var logFile = self.output;
                var encoded = encodeURIComponent(logFile);
                file += encoded;
                var a = document.createElement('a');
                a.href = file;
                a.target   = '_blank';
                a.download = downloadFileName;
                document.body.appendChild(a);
                a.click();
                a.remove();
            }
    

    reply
    0
  • Cancelreply