-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileUpload.js
70 lines (68 loc) · 2.23 KB
/
FileUpload.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { useState } from "react";
import axios from "axios";
import "./FileUpload.css";
const FileUpload = ({ contract, account, provider }) => {
const [file, setFile] = useState(null);
const [fileName, setFileName] = useState("No image selected");
const handleSubmit = async (e) => {
e.preventDefault();
if (file) {
try {
const formData = new FormData();
formData.append("file", file);
const resFile = await axios({
method: "post",
url: "https://api.pinata.cloud/pinning/pinFileToIPFS",
data: formData,
headers: {
pinata_api_key: ' Enter you api key ',// genrated from your pinata account
pinata_secret_api_key: 'Enter your secret api key ', // generated by your ;pinata account
"Content-Type": "multipart/form-data",
},
});
const ImgHash = `https://gateway.pinata.cloud/ipfs/${resFile.data.IpfsHash}`;
contract.add(account,ImgHash);
alert("Successfully Image Uploaded");
setFileName("No image selected");
setFile(null);
} catch (e) {
alert("Unable to upload image to Pinata");
}
}
// alert("Successfully Image Uploaded");
setFileName("No image selected");
setFile(null);
};
const retrieveFile = (e) => {
const data = e.target.files[0]; //files array of files object
// console.log(data);
const reader = new window.FileReader();
reader.readAsArrayBuffer(data);
reader.onloadend = () => {
setFile(e.target.files[0]);
};
setFileName(e.target.files[0].name);
e.preventDefault();
};
return (
<div className="top">
<form className="form" onSubmit={handleSubmit}>
<label htmlFor="file-upload" className="choose">
Choose Image
</label>
<input
disabled={!account}
type="file"
id="file-upload"
name="data"
onChange={retrieveFile}
/>
<span className="textArea">Image: {fileName}</span>
<button type="submit" className="upload" disabled={!file}>
Upload File
</button>
</form>
</div>
);
};
export default FileUpload;