ie 的 大坑 不支持 的時間格式 yyyy-mm-dd 和 不支持 URL.createObjectURL(blob) 進行文件下載


1.  ie 支持的時間格式是不支持  yyyy-mm-dd 的
  需要手動進行格式化

  

    private praseDate(d: Date): string {

        // 坑比 ie 不支持 yyyy-mm-dd 格式
        // return moment(d).format('YYYY-MM-DD');


        let year = d.getFullYear();
        let month = d.getMonth() + 1;
        let date = d.getDate();

        let monthStr = '';
        let dataStr = '';

        if (month < 10) {
            monthStr = '0' + month;
        } else {
            monthStr = month + '';
        }
        if (date < 10) {
            dataStr = '0' + date;
        } else {
            dataStr = date + '';
        }
        console.log('praseDate' + year + '-' + monthStr + '-' + dataStr);

        return year + '-' + monthStr + '-' + dataStr;
    }

  2.  使用  blob window.URL.createObjectURL 方式下 載文件 時 , 在ie 上 是 拒絕訪問的~~~~~

  const blob = new Blob([res.body ], {type: 'text/csv'});
    var url = window.URL.createObjectURL(blob);
     var a = document.createElement('a');
           document.body.appendChild(a);
            a.setAttribute('style', 'display: none');
            a.href = url;
            a.download = fileName + '.csv';
            a.click();
            window.URL.revokeObjectURL(url);
            a.remove();

  添加上 對於 ie 判斷  通過   ie  的 標准 window.navigator.msSaveOrOpenBlob(blob, fileName);

  

  console.log('start download:', res);
            const blob = new Blob([res.body ], {type: 'text/csv'});

            //  坑爹 ie 不支持
            if('msSaveOrOpenBlob' in navigator){
                window.navigator.msSaveOrOpenBlob(blob, fileName);
                return;
            }
            var url = window.URL.createObjectURL(blob);
            var a = document.createElement('a');
            document.body.appendChild(a);
            a.setAttribute('style', 'display: none');
            a.href = url;
            a.download = fileName + '.csv';
            a.click();
            window.URL.revokeObjectURL(url);
            a.remove();

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM