How to set a file name using window.open

前端 未结 4 1725
孤独总比滥情好
孤独总比滥情好 2020-11-28 05:44

I\'am trying to download temporary result calculated by JavaScript. Say I have a string str, I want to download a file contains str and named it as

4条回答
  •  迷失自我
    2020-11-28 06:04

    A solution for IE is to use msSaveBlob and pass the file name.

    For modern browsers solution goes like this, tested: IE11, FF & Chrome

     var csvData = new Blob([arg.data], {type: 'text/csv;charset=utf-8;'});
            //IE11 & Edge
            if (navigator.msSaveBlob) {
                navigator.msSaveBlob(csvData, exportFilename);
            } else {
                //In FF link must be added to DOM to be clicked
                var link = document.createElement('a');
                link.href = window.URL.createObjectURL(csvData);
                link.setAttribute('download', exportFilename);
                document.body.appendChild(link);    
                link.click();
                document.body.removeChild(link);    
            }
    

    There is a good discution about that here

提交回复
热议问题