change download file name

This commit is contained in:
Mohamed Nouffer
2021-11-15 17:08:28 +05:30
parent 7581d4c441
commit 6168ec9123
9 changed files with 34 additions and 18 deletions

View File

@ -120,12 +120,28 @@ function Index() {
method: 'GET',
responseType: 'blob', // important
}).then((response) => {
const url = window.URL.createObjectURL(new Blob([response.data]));
const link = document.createElement('a');
link.href = url;
//link.setAttribute('download', 'file.xls'); //or any other extension
//document.body.appendChild(link);
link.click();
// const url = window.URL.createObjectURL(new Blob([response.data]));
// const link = document.createElement('a');
// link.href = url;
// //link.setAttribute('download', 'file.xls'); //or any other extension
// //document.body.appendChild(link);
// link.click();
const blob = new Blob([response.data], {type: response.data.type});
const url = window.URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
const contentDisposition = response.headers['content-disposition'];
let fileName = 'unknown';
if (contentDisposition) {
const fileNameMatch = contentDisposition.match(/filename="(.+)"/);
if (fileNameMatch.length === 2)
fileName = fileNameMatch[1];
}
link.setAttribute('download', fileName);
document.body.appendChild(link);
link.click();
link.remove();
window.URL.revokeObjectURL(url);
});
}