UNPKG

@builton/core-sdk

Version:
35 lines (34 loc) 1.11 kB
<!DOCTYPE html> <html> <head> <script src="../../dist/main.bundle.js"></script> </head> <body> <form id="uploadForm"> <input id="image" type="file" name="file" accept="image/gif,image/jpeg,image/png,image/jpg"/> <button type="submit">Upload</button> </form> <div id="imageContainer" style="max-width:'300px'"></div> </body> <script> const apiKey = 'YOUR_API_KEY'; const b = new Builton({ apiKey, bearerToken: 'SOME_BEARER_TOKEN', }); async function handleFileSelect(e) { e.preventDefault(); const imageData = document.getElementById("image").files[0]; try { const image = await b.images.create(imageData, { isPublic: true }); const imageUrl = "https://api.builton.dev/images/" + image.url + "?api_key=" + apiKey; document.getElementById("imageContainer").innerHTML = "<img src=\"" + imageUrl + "\" />"; const user = b.users.setMe(); user.update({ avatar: image.url }); } catch(e) { console.error(e); } } document.getElementById("uploadForm").onsubmit = function(e) { handleFileSelect(e); }; </script> </html>