blockchain:dappsからipfsにファイルをアップロードする
差分
このページの2つのバージョン間の差分を表示します。
両方とも前のリビジョン前のリビジョン次のリビジョン | 前のリビジョン | ||
blockchain:dappsからipfsにファイルをアップロードする [2022/06/12 11:29] – dot | blockchain:dappsからipfsにファイルをアップロードする [2022/06/16 12:54] (現在) – dot | ||
---|---|---|---|
行 19: | 行 19: | ||
* [[https:// | * [[https:// | ||
+ | * [[https:// | ||
===== IPFSUploader.tsx の実装 ===== | ===== IPFSUploader.tsx の実装 ===== | ||
行 30: | 行 31: | ||
<code JavaScript> | <code JavaScript> | ||
- | import React, { BaseSyntheticEvent, | + | import React, { useState, useEffect } from ' |
import type { IPFS } from ' | import type { IPFS } from ' | ||
import * as IPFSCore from ' | import * as IPFSCore from ' | ||
行 69: | 行 70: | ||
setIpfsReady(Boolean(ipfs)); | setIpfsReady(Boolean(ipfs)); | ||
}; | }; | ||
- | const fileSelected = async (event: | + | const fileSelected = async (event: |
if(!isIpfsReady) return; | if(!isIpfsReady) return; | ||
- | const targetFile = event.target.files[0]; | + | const targetFile = event.target.files?.[0]; |
- | const _result = await ipfs.add(targetFile); | + | |
- | //const _result = await ipfs.add(' | + | |
- | setCID(_result.path); | + | //const _result = await ipfs.add(' |
+ | setCID(_result.path); | ||
+ | } | ||
}; | }; | ||
行 133: | 行 136: | ||
MFS を利用してファイルをアップロードする。 | MFS を利用してファイルをアップロードする。 | ||
+ | <code PowerShell> | ||
+ | IPFS\ipfs-web> | ||
+ | </ | ||
<code JavaScript> | <code JavaScript> | ||
- | import React, { BaseSyntheticEvent, | + | import React, { useState, useEffect } from ' |
import type { IPFS } from ' | import type { IPFS } from ' | ||
import * as IPFSCore from ' | import * as IPFSCore from ' | ||
行 188: | 行 194: | ||
setIpfsReady(Boolean(ipfs)); | setIpfsReady(Boolean(ipfs)); | ||
}; | }; | ||
- | const fileSelected = async (event: | + | const fileSelected = async (event: |
if(!isIpfsReady) return; | if(!isIpfsReady) return; | ||
- | const targetFile = event.target.files[0]; | + | const targetFile = event.target.files?.[0]; |
- | const fileReader = new window.FileReader(); | + | |
- | fileReader.readAsArrayBuffer(targetFile); | + | |
- | fileReader.onloadend = async () => { | + | fileReader.readAsArrayBuffer(targetFile); |
- | let ab = fileReader.result as ArrayBuffer; | + | fileReader.onloadend = async () => { |
- | const u8a = new Uint8Array(ab); | + | let ab = fileReader.result as ArrayBuffer; |
- | console.log(`upload / | + | const u8a = new Uint8Array(ab); |
- | await ipfs.files.write(`/ | + | console.log(`upload / |
- | await ls("/ | + | await ipfs.files.write(`/ |
- | }; | + | await ls("/ |
+ | }; | ||
+ | } | ||
}; | }; | ||
const ls = async (path: string) => { | const ls = async (path: string) => { | ||
行 232: | 行 240: | ||
< | < | ||
<p> | <p> | ||
- | <a href={`https:// | + | <a href={`https:// |
<img src={`https:// | <img src={`https:// | ||
</p> | </p> | ||
行 245: | 行 253: | ||
export default IPFSDirectoryUploader; | export default IPFSDirectoryUploader; | ||
</ | </ | ||
+ | |||
+ | App.tsx に IPFSDirectoryUploader コンポーネントを記述する。 |
blockchain/dappsからipfsにファイルをアップロードする.1655033394.txt.gz · 最終更新: 2022/06/12 11:29 by dot