open-vault/ui/app/services/download-csv.js
claire bontempo 10a19b7c54
UI/ fix firefox not recognizing csv export (#15364)
* add extension to filenmae

* add changelog
2022-05-10 17:19:38 -07:00

27 lines
1.1 KiB
JavaScript

import Service from '@ember/service';
// SAMPLE CSV FORMAT ('content' argument)
// Must be a string with each row \n separated and each column comma separated
// 'Namespace path,Authentication method,Total clients,Entity clients,Non-entity clients\n
// namespacelonglonglong4/,,191,171,20\n
// namespacelonglonglong4/,auth/method/uMGBU,35,20,15\n'
export default class DownloadCsvService extends Service {
download(filename, content) {
// even though Blob type 'text/csv' is specified below, some browsers (ex. Firefox) require the filename has an explicit extension
let formattedFilename = `${filename?.replace(/\s+/g, '-')}.csv` || 'vault-data.csv';
let { document, URL } = window;
let downloadElement = document.createElement('a');
downloadElement.download = formattedFilename;
downloadElement.href = URL.createObjectURL(
new Blob([content], {
type: 'text/csv',
})
);
document.body.appendChild(downloadElement);
downloadElement.click();
URL.revokeObjectURL(downloadElement.href);
downloadElement.remove();
}
}