xxxxxxxxxx
export const convertFileToBase64 = (file) => {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = () => {
const base64Data = reader.result.split(',')[1]; // Extract the base64 data part
resolve(base64Data);
};
reader.onerror = (error) => {
reject(error);
};
// Read the file as a data URL
reader.readAsDataURL(file);
});
};
xxxxxxxxxx
const url = 'data:image/png;base6....';
fetch(url)
.then(res => res.blob())
.then(blob => {
const file = new File([blob], "File name",{ type: "image/png" })
})
xxxxxxxxxx
handleUpload(event) {
const file = event.target.files[0];
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = () => {
console.log(reader.result);
};
}
xxxxxxxxxx
export const base64ToFile = (base64String, fileName, mimeType) => {
const blob = base64ToBlob(base64String, mimeType);
return new File([blob], fileName, { type: mimeType });
};
xxxxxxxxxx
<a [href]="'data:application/octet-stream;base64,' + data | safe" download="excel.xls">Download</a>