getBlobAndRenderAsImageREADME.md11 matches
1# getBlobAndRenderAsImage
23Shows how to get a Val Town blob and render it as an image in the DOM with React.
45## Setup
67Make sure you have an image named `test.png` in your Val Town blob storage (alternatively, you can change the key name in the blob getter in the script).
89To easily upload an image to your blob storage, [fork this val](
10getBlobAndRenderAsImage), run it, and enter your API key in the password input.
1112## How it works
141. Fetching the blob:
1516- The client-side React component makes a fetch request to "/image".
17- This request is handled by our server function.
1822- The server function calls `blob.get("test.png")`.
23- This `blob.get()` method makes an HTTP request to the Val Town API.
24- The API returns a Response object containing the image data.
2526344. Sending the response to the client:
3536- We set the appropriate "Content-Type" header (e.g., "image/png").
37- We return the Response object to the client.
3848- We create a FileReader object.
49- We use FileReader to read the Blob as a data URL.
50- This data URL is a base64-encoded string representing the image.
51525657588. Rendering the image:
5960- We use the data URL as the src attribute of an `<img>` tag.
61- The browser decodes the base64 data and renders the image.
6263## Further reading on Val Town blobs
blob_adminREADME.md1 match
3This is a lightweight Blob Admin interface to view and debug your Blob data.
45
67Use this button to install the val:
ablePinkDogREADME.md1 match
3Protect your vals behind a password. Use session cookies to persist authentication.
45
67## Demo
3This is a lightweight Blob Admin interface to view and debug your Blob data.
45
67Use this button to install the val:
3This is a lightweight Blob Admin interface to view and debug your Blob data.
45
67Use this button to install the val:
blob_adminREADME.md1 match
3This is a lightweight Blob Admin interface to view and debug your Blob data.
45
67Use this button to install the val:
141142143// serves to be displayed in browser instead of download (eg for images, PDFs)
144async serve(key: string, rangeHeader?: string): Promise<Response> {
145const stored = await blob.get(this.getKey(key));
221case "jpg":
222case "jpeg":
223return "image/jpeg";
224case "png":
225return "image/png";
226case "mp3":
227return "audio/mpeg";
72| "ports-spec"
73/**
74* Indicates that the input or output port represents an image. The image can
75* be a URL or a base64 encoded image.
76*/
77| "image"
78/**
79* Indicates that the input or output represents some sort of code
165. If the user is logged in, you now have a email you can work with. If not, it will be empty
1718
1920[Live demo](https://stevekrouse-lastlogin_demo.web.val.run/)