Uploading an Image to IPFS

Audience:

IPFS Primer:

Normal Uploading:

Browser to IPFS:

Project Setup:

  1. ipfs init
  2. ipfs daemon
  1. Stop IPFS with ctrl-c
  2. ipfs config -- json API.HTTPHeaders.Access-Control-Allow-Methods ‘[“PUT”, “GET”, “POST”, “OPTIONS”]’
  3. ipfs config — json API.HTTPHeaders.Access-Control-Allow-Origin ‘[“*”]’
  4. ipfs daemon

Hosting a Website:

Using node to host website:

  1. npm install http-server -g
  2. http-server -p 1337

Using Python to host a website:

  1. python -m SimpleHTTPServer 1337

Using the file system to host a website:

Example:

  1. Create HTML input field with id="photo"
  2. Create HTML button with onclick="upload()"
  3. upload() creates instance const reader = new FileReader()
  4. Give reader.readAsArrayBuffer the file inside id="photo" element
  5. Bind to reader event emitter method onloaded
  6. Initialize ipfs object bound to local IPFS node on port 5001
  7. Create buffer of the image that was read from reader.result
  8. Call ipfs.files.add with buffer with callback function
  9. Read result variable
  10. Create the url string
  11. Modify data on DOM directly

API Details:

FileReader:

Buffer:

JS-IPFS-API:

Considerations:

Conclusion:

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store