Created
November 8, 2020 09:44
-
-
Save danopia/1a91ea95882254872f50eb2d62210657 to your computer and use it in GitHub Desktop.
Small Deno sample of downloading a binary file into a data URL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as Base64 from 'https://deno.land/[email protected]/encoding/base64.ts'; | |
async function toDataUrl(resp: Response): Promise<string> { | |
const contentType = resp.headers.get('content-type') ?? 'application/octet-stream'; | |
const buffer = await resp.arrayBuffer(); | |
return `data:${contentType};base64,`+Base64.encode(buffer); | |
} | |
const gifUrl = 'https://matthews.sites.wfu.edu/misc/jpg_vs_gif/testImage.gif'; | |
console.log(await fetch(gifUrl).then(toDataUrl)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To address the mix of await and then:
Just a personal preference