Skip to content

Instantly share code, notes, and snippets.

@prince-curie
Last active July 17, 2022 14:31
Show Gist options
  • Select an option

  • Save prince-curie/d72baad9b0cc0d2bc8d5f25f532a75a8 to your computer and use it in GitHub Desktop.

Select an option

Save prince-curie/d72baad9b0cc0d2bc8d5f25f532a75a8 to your computer and use it in GitHub Desktop.
Uploading an image to IPFS with html and javascript
<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