Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
June 28, 2021 01:30 pm GMT

Uploading Files to IPFS from a Web Application

One of the most common interactions with IPFS is uploading files like images and videos from a client-side application, so I found it surprising that there were not a lot of straightforward tutorials showing how this is done.

In this tutorial you will learn just that in as few lines of code (and as simply) as possible using ipfs-http-client. The ideas here are implemented in React but should be fairly easily transferrable to doing the same thing in any other JavaScript framework, like Vue, Angular, or Svelte.

About IPFS

IPFS is a decentralized, peer to peer file sharing protocol.

There are various types of IPFS gateways available. Some are free, some are not. Some offer read-only access, and others offer both read and write access.

You can also run your own IPFS gateway.

Because we will be uploading / saving files, we need to be sure to choose a gateway that allows us write access. The gateway we will be using today is Infura. Other popular services are Pinata or Fleek.

For an example of how to pin a file to IPFS with Pinata, check out this repo.

Getting started

If you already have a React application created, you can skip this step.

First, create a new React app and change into the new directory:

npx create-react-app ipfs-examplecd ipfs-example

Next, install the ipfs-http-client library using either NPM or Yarn:

npm install ipfs-http-client

Base code

The basic functionality can be summed up in only 3 lines of code, but I'll also be building out an entire UI to show how it all fits together.

The basic code for getting this to work is here:

/* import the ipfs-http-client library */import { create } from 'ipfs-http-client';/* Create an instance of the client */const client = ipfsHttpClient('https://ipfs.infura.io:5001/api/v0')/* upload the file */const added = await client.add(file)/* or a string */const added = await client.add('hello world')

Full code

Let's now look at how the above code would be used to actually implement file upload functionality in our app for uploading and viewing images.

In your project, open src/App.js and update it with the following code:

/* src/App.js */import './App.css'import { useState } from 'react'import { create } from 'ipfs-http-client'const client = create('https://ipfs.infura.io:5001/api/v0')function App() {  const [fileUrl, updateFileUrl] = useState(``)  async function onChange(e) {    const file = e.target.files[0]    try {      const added = await client.add(file)      const url = `https://ipfs.infura.io/ipfs/${added.path}`      updateFileUrl(url)    } catch (error) {      console.log('Error uploading file: ', error)    }    }  return (    <div className="App">      <h1>IPFS Example</h1>      <input        type="file"        onChange={onChange}      />      {        fileUrl && (          <img src={fileUrl} width="600px" />        )      }    </div>  );}export default App

Next, run the app:

npm start

When the app loads, you should see a file upload button.

Once a file has been successfully uploaded, you should see it rendered in the UI.


Original Link: https://dev.to/dabit3/uploading-files-to-ipfs-from-a-web-application-50a

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To