Typescript blob filename without link

后端 未结 2 1615
既然无缘
既然无缘 2020-12-31 17:21

How to set file name for blob in typescript? For IE, I can setup file name easily but for Chrome it looks impossible. Basically I need something similar to this solution but

2条回答
  •  执笔经年
    2020-12-31 17:50

    Here is the download method working on IE, chrome and firefox:

      downloadCsvFile(data, fileName) {
        const csvName = fileName +  '.csv';
        const blob = new Blob([data], {type: 'text/csv'});
        if (window.navigator && window.navigator.msSaveOrOpenBlob) { // IE
          window.navigator.msSaveOrOpenBlob(blob, csvName);
          window.navigator.msSaveOrOpenBlob(blob, csvName);
        } else { //Chrome & Firefox
          const a = document.createElement('a');
          const url = window.URL.createObjectURL(blob);
          a.href = url;
          a.download = csvName;
          a.click();
          window.URL.revokeObjectURL(url);
          a.remove();
        }
      }
    

提交回复
热议问题