Skip to content

Instantly share code, notes, and snippets.

@stevedylandev
Created April 27, 2022 15:57
Show Gist options
  • Save stevedylandev/6ac80b5eb9736fb29d9056f4440e71f1 to your computer and use it in GitHub Desktop.
Save stevedylandev/6ac80b5eb9736fb29d9056f4440e71f1 to your computer and use it in GitHub Desktop.
Pinata React Upload Component
import { useState } from "react"
import axios from "axios"
const FileUpload = () => {
const [selectedFile, setSelectedFile] = useState();
const changeHandler = (event) => {
setSelectedFile(event.target.files[0]);
};
const handleSubmission = async() => {
const formData = new FormData();
formData.append('file', selectedFile)
const metadata = JSON.stringify({
name: 'File name',
});
formData.append('pinataMetadata', metadata);
try{
const res = await axios.post("https://api.pinata.cloud/pinning/pinFileToIPFS", formData, {
maxBodyLength: "Infinity",
headers: {
'Content-Type': `multipart/form-data; boundary=${formData._boundary}`,
Authorization: `Bearer PASTE_YOUR_JWT_HERE`,
}
});
console.log(res.data);
} catch (error) {
console.log(error);
}
};
return (
<>
<label class="form-label">Choose File</label>
<input type="file" onChange={changeHandler}/>
<button onClick={handleSubmission}>Submit</button>
</>
)
}
export default FileUpload
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment