change download file name
This commit is contained in:
@ -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);
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user