Convert base64 string into File type
const url = 'data:image/png;base6....'; fetch(url) .then(res => res.blob()) .then(blob => { const file = new File([blob], "File name",{ type: "image/png" }) })
Source: dev.to
Convert file to Base64
handleUpload(event) { const file = event.target.files[0]; const reader = new FileReader(); reader.readAsDataURL(file); reader.onload = () => { console.log(reader.result); }; }
Source: stackoverflow.com
base64 from file
convertFile(file: File): Observable<string> { const result = new ReplaySubject<string>(1); const reader = new FileReader(); reader.readAsBinaryString(file); reader.onload = (event) => result.next(btoa(reader.result.toString())); return result; } this.convertFile(event.target.files[0]).subscribe(base64 => { this.base64Output = base64; }); /// NOTE /// // The event.target.files is just the File Object e.g. from a // <input type="file"> form // you can also create a file with the following command: var f = new File([""], "filename");
save base 64 string to file
<a [href]="'data:application/octet-stream;base64,' + data | safe" download="excel.xls">Download</a>
Source: stackoverflow.com