23
loading...
This website collects cookies to deliver better user experience
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.If you already have a React application created, you can skip this step.
npx create-react-app ipfs-example
cd ipfs-example
ipfs-http-client
library using either NPM or Yarn:npm install ipfs-http-client
/* 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')
/* 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
npm start