Created
          July 19, 2022 11:06 
        
      - 
      
- 
        Save stevedylandev/9cd0e82b93fdb8ed485f9bb6baceef7d to your computer and use it in GitHub Desktop. 
    A React component for uploading folders to Pinata
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | import { useState } from "react" | |
| import axios from "axios" | |
| const FolderUpload = () => { | |
| const [selectedFile, setSelectedFile] = useState(); | |
| const changeHandler = (event) => { | |
| setSelectedFile(event.target.files); | |
| }; | |
| const handleSubmission = async() => { | |
| const formData = new FormData(); | |
| Array.from(selectedFile).forEach((file) => { | |
| formData.append("file", file) | |
| }) | |
| const metadata = JSON.stringify({ | |
| name: 'Folder name', | |
| }); | |
| formData.append('pinataMetadata', metadata); | |
| const options = JSON.stringify({ | |
| cidVersion: 1, | |
| }) | |
| formData.append('pinataOptions', options); | |
| 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 Folder</label> | |
| <input directory="" webkitdirectory="" type="file" onChange={changeHandler}/> | |
| <button onClick={handleSubmission}>Submit</button> | |
| </> | |
| ) | |
| } | |
| export default FolderUpload | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment