1) 개념
Blob은 Binary Large Object의 약자로 이진 데이터를 나타내는 데 사용되는 파일 형태입니다.
주로 이미지, 오디오, 비디오, 문서 등과 같은 다양한 형식의 이진 데이터를 나타내게 됩니다.
2) 활용
이를 이전에 설명드린
fetch.then.then.catch 형태에서 첫번째
.then 에서 통신이 성공했을 때
blob 파일 형태로 넘기게 됩니다.
웹 애플리케이션에서는 Ajax요청이나 formData객체를 활용하여 전송하게 됩니다.
2-1) XMLHttpRequest
var xhr = new XMLHttpRequest();
xhr.open('GET', '요청을 전송할 url', true);
xhr.responseType = 'blob';
xhr.onload = function () {
if (xhr.status === 200) {
var blobData = xhr.response;
}
};
xhr.send();
2-2) Fetch API
fetch('요청을 전송할 url')
.then(response => response.blob())
.then(blobData => {
})
.catch(error => console.error('Error fetching blob:', error));
var formData = new FormData();
formData.append('file', blobData, 'filename.txt');
fetch('요청을 전송할 url', {
method: 'POST',
body: formData
})
.then(response => {
return response.blob();
})
.then(blob => {
})
.catch(error => console.error('Error uploading blob:', error));