Last active
July 17, 2022 14:31
-
-
Save prince-curie/d72baad9b0cc0d2bc8d5f25f532a75a8 to your computer and use it in GitHub Desktop.
Uploading an image to IPFS with html and javascript
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
| <html> | |
| <body> | |
| <form> | |
| <input type="file" accept="image/*" name="image" id="file"> | |
| </form> | |
| <script> | |
| document.querySelector('#file').addEventListener('change', upload) | |
| async function upload() { | |
| const fileReader = new FileReader() | |
| // Read file as ArrayBuffer | |
| await fileReader.readAsArrayBuffer(event.target.files[0]) | |
| // Listen for the onload event | |
| fileReader.onload = async (event) => { | |
| const node = await Ipfs.create() | |
| // upload the file content | |
| let { path } = await node.add(fileReader.result) | |
| console.log(path) | |
| } | |
| } | |
| </script> | |
| <script src="https://cdn.jsdelivr.net/npm/ipfs/dist/index.min.js"></script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment