I am trying to download a file in Angular. The file is saved in db as varbinary. Java REST service fetched it as byte[]. I am having it as Int8Array in Angular. However when I download it I beleive it is base64 encoded
const link = document.createElement( 'a' ); link.style.display = 'none'; document.body.appendChild( link ); const blob = new Blob([myFileByteArray], {type: 'text/xlsx'}); //myFile is Int8Array const objectURL = URL.createObjectURL(blob); link.href = objectURL; link.href = URL.createObjectURL(blob); link.download = this.myfile.name; link.click();
This is how it is in MSSQL: 0x323032312D30392D323720303 ...
And this is how it is when I download this xlsx and open it: MjAyMS0wOS0yNyAwNzozMDsxMi4wODI7bT
I beleive it is base64 encoded somewhere in that path from sql to browser…I saved it in a SQL like this 'CAST('this is my xslx' AS VARBINARY (MAX))
so I know it should be this text.
Advertisement
Answer
The solution was to change a type in Angular from Int8Array to string and then I could use atob() method to decode from base64. I just don’t know why is it in base64. Could it be because I use Spring Boot ResponseEntity …
this.myFile= this.myFileResultSet.result; let myFileByteArray = this.myFile.myFileBytes //before Int8Array, now String console.log(myFileByteArray); let myFileDecoded = atob(myFileByteArray); // must be string not Int8Array to be able to // convert it from base64 const link = document.createElement( 'a' ); link.style.display = 'none'; document.body.appendChild( link ); const blob = new Blob([myFileDecoded], {type: 'text/csv'}); const objectURL = URL.createObjectURL(blob); link.href = objectURL; link.href = URL.createObjectURL(blob); link.download = this.myFile.name; link.click(); });