Advertisement
yarin0600

Untitled

Jan 11th, 2024
876
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const [fileContent, setFileContent] = useState(null);
  2.  
  3.   const handleFileChange = (event) => {
  4.     const file = event.target.files[0];
  5.  
  6.     if (file) {
  7.       const reader = new FileReader();
  8.  
  9.       reader.onload = (e) => {
  10.         const content = e.target.result;
  11.         setFileContent(content);
  12.       };
  13.  
  14.       reader.readAsArrayBuffer(file);
  15.     }
  16.   };
  17.  
  18.   const pinFileToIPFS = async () => {
  19.     try {
  20.       if (!fileContent) {
  21.         console.error('No file selected');
  22.         return;
  23.       }
  24.  
  25.       const formData = new FormData();
  26.       const blob = new Blob([fileContent]);
  27.       formData.append('file', blob);
  28.  
  29.       // Now you can use formData for your API request, e.g., send it to the server or IPFS
  30.  
  31.       // Example using fetch:
  32.       // const response = await fetch('your-api-endpoint', {
  33.       //   method: 'POST',
  34.       //   body: formData,
  35.       // });
  36.  
  37.       // Handle the response as needed
  38.     } catch (error) {
  39.       console.error('Error pinning file to IPFS:', error);
  40.     }
  41.   };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement