1. Get the file
const options = {
method: "GET",
headers: {
Accept: "application/json",
Authorization: "Bearer {API_Key}",
},
};
const response = await fetch("https://api.upsend.app/files/{id}", options);
// Add type guard
const file = (await response.json()) as {
id: string;
created: string;
updated: string;
user_id: string;
name: string;
size: string;
mime_type: string;
buffer: Buffer;
};
return {
buffer: file.buffer,
name: file.name,
mime_type: file.mime_type
}
2. Download the file
function download(
buffer: Buffer,
name: string,
mime_type: string,
): void {
try {
const blob = new Blob([new Uint8Array(buffer)], { type: mime_type });
const url = URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = url;
link.download = name;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
} catch (e) {
console.error(e);
}
}
3. Show images
For images the prefered way is to use Images API. But If there is still a need to show image using Files API, You can use the generated url to show it:
<script>
...
const blob = new Blob([new Uint8Array(buffer)], { type: mimeType });
const url = URL.createObjectURL(blob);
</script>
<img style='display:block; width:100px;height:100px;' src={url} />