Skip to content

Commit 524572e

Browse files
authored
Merge pull request #368 from plamber/dev
Shows how to download photos with batching and prepare them for rendering in a browser
2 parents 96b40bd + 3c729a2 commit 524572e

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

docs/content/Batching.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,81 @@ const serialBatching = async function(elem) {
6666
};
6767
```
6868

69+
### Download multiple profile photos with batching and preprocess these for rendering in a browser
70+
71+
You should convert the downloaded photos through batching to a Base64 representation if you want to render these in a browser.
72+
73+
```typescript
74+
b64toBlob = async (b64Data:any, contentType:string, sliceSize?:number):Promise<Blob> => {
75+
contentType = contentType || 'image/png';
76+
sliceSize = sliceSize || 512;
77+
78+
let byteCharacters:string = atob(b64Data);
79+
let byteArrays = [];
80+
81+
for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) {
82+
let slice = byteCharacters.slice(offset, offset + sliceSize);
83+
84+
let byteNumbers = new Array(slice.length);
85+
for (let i = 0; i < slice.length; i++) {
86+
byteNumbers[i] = slice.charCodeAt(i);
87+
}
88+
89+
let byteArray = new Uint8Array(byteNumbers);
90+
byteArrays.push(byteArray);
91+
}
92+
93+
let blob = new Blob(byteArrays, {type: contentType});
94+
return blob;
95+
};
96+
97+
blobToBase64 = (blob: Blob): Promise<string> => {
98+
return new Promise((resolve, reject) => {
99+
const reader = new FileReader();
100+
reader.onerror = reject;
101+
reader.onload = _ => {
102+
resolve(reader.result as string);
103+
};
104+
reader.readAsDataURL(blob);
105+
});
106+
};
107+
108+
downloadPhotosBatching = async (client: Client) => {
109+
try {
110+
111+
112+
// create batch request steps for the users specified above
113+
const batchRequestSteps : BatchRequestStep[] = users.map((userId) => {
114+
const request : BatchRequestStep = {
115+
id: userId,
116+
request: new Request(`/users/${userId}/photo/$value`, {
117+
method: "GET",
118+
})
119+
};
120+
return request;
121+
})
122+
123+
// initiate the batchrequest and execute the operation
124+
const batchRequestContent = new BatchRequestContent(batchRequestSteps);
125+
const content = await batchRequestContent.getContent();
126+
const batchResponse = new BatchResponseContent(await client.api("/$batch").post(content));
127+
128+
// example on how to retrieve the base64 representation of the downloaded image for the first user
129+
const response = batchResponse.getResponseById(users[0]);
130+
if (response.ok) {
131+
var data = await response.text();
132+
const binToBlob = await this.b64toBlob((data),'img/jpg');
133+
134+
// you can associate the base64 output to an src attribute of an <img> HTML tag
135+
const base64Result = await this.blobToBase64(binToBlob);
136+
console.log(base64Result);
137+
}
138+
} catch (error) {
139+
console.error(error);
140+
}
141+
};
142+
```
143+
69144
### GET and POST contents from and to different workloads - Making parallel requests
70145

71146
```typescript

0 commit comments

Comments
 (0)