sandbox-react/jszip-dev/src/utils/download.js

74 lines
2.2 KiB
JavaScript

export function getBlob(url,fileName) {
console.log('getBlob() url: ', url);
console.log('getBlob() fileName: ', fileName);
fetch(url, {
method: 'GET',
headers: {
'Content-Type': "text/csv",
},
})
.then((response) => response.blob())
.then((blob) => {
return blob;
//TODO Cleanup! createDloadLink(new Blob([blob], { type: "text/csv" }),fileName);
});
};
export async function getBlobAsync(url,fileName) {
console.log('getBlob() url: ', url);
console.log('getBlob() fileName: ', fileName);
const rsp = await fetch(url, {
method: 'GET',
headers: {
'Content-Type': "text/csv",
},
});
console.log('getBlob() done');
return rsp;
//TODO Cleanup! createDloadLink(new Blob([blob], { type: "text/csv" }),fileName);
};
/*
* download file
* @param url address used to fetch source
* @param fileName name of file to be downloaded as string
*/
export function createDloadLinkByUrl(url,fileName) {
////console.log('download:createDloadLInkByUrl start...');
////console.log('download:createDloadLInkByUrl url: '+url);
////console.log('download:createDloadLInkByUrl fileName: '+fileName);
fetch(url, {
method: 'GET',
headers: {
'Content-Type': "text/csv",
},
})
.then((response) => response.blob())
.then((blob) => {
createDloadLink(new Blob([blob], { type: "text/csv" }),fileName);
});
////console.log('download:createDloadLInkByUrl done.');
};
/*
* download file
* @param blob to be downloaded
* @param fileName name of file to be downloaded as string
*/
export function createDloadLink(blob,fileName) {
////console.log('download:createDloadLink() start...');
////console.log('download:createDloadLink() fileName: '+fileName);
// Create blob link to download
const url = window.URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
link.target = "_blank";
link.download = fileName;
// Append to html link element page
document.body.appendChild(link);
// Start download
link.click();
// Clean up and remove the link
link.parentNode.removeChild(link);
////console.log('download:createDloadLink() done.');
};