Created
          April 27, 2022 15:57 
        
      - 
      
- 
        Save stevedylandev/6ac80b5eb9736fb29d9056f4440e71f1 to your computer and use it in GitHub Desktop. 
    Pinata React Upload Component
  
        
  
    
      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 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