Programming/JavaScript
Base64를 Blob 또는 File 객체로 변환하기
junnnhhh
2024. 1. 25. 22:38
728x90
Base64 인코딩은 Binary Data를 Text로 변경하는 인코딩이다.
atob() → Base64 인코딩된 문자열 데이터를 디코딩한다.
atob(encodedData);
// base64 인코딩된 데이터를 담은 이진 문자열
charCodeAt() → index에 해당하는 문자의 unicode 값을 리턴한다.
"ABC".charCodeAt(0);
// "ABC"에 0번째 인덱스인 "A"의 unicode 값을 리턴한다.
var bs64 = data;
const byteCharacters = atob(bs64);
const byteNumbers = new Array(byteCharacters.length);
for(let i=0; i<byteCharacters.length; i++) {
byteNumbers[i] = byteCharacters.charCodeAt(i);
}
const byteArray = new Uint8Array(byteNumbers);
const blob = new Blob([byteArray], {type: 'image/png'});
const file = new File([byteArray], 'filename', {type: 'image/png'});
.base64를 binary 문자열로 인코딩 한 후, 해당 문자의 unicode를 저장한 배열을 만든다.
728x90