File Upload And Metadata Updation In Spfx With Pnpjs(React)

Open a command prompt. Create a directory for SPFx solution.
 
md spfx-React-PnpFileUpload
 
Navigate to the above created directory.
 
cd spfx-React-PnpFileUpload
 
Run the Yeoman SharePoint Generator to create the solution.
 
yo @microsoft/sharepoint
 
Solution Name
 
Hit Enter to have default name (spfx-pnp-DropDown in this case) or type in any other name for your solution.
Selected choice - Hit Enter
 
Target for the component
 
Here, we can select the target environment where we are planning to deploy the client web part, i.e., SharePoint Online or SharePoint OnPremise (SharePoint 2016 onwards).
 
Selected choice: SharePoint Online only (latest)
 
Place of files
 
We may choose to use the same folder or create a subfolder for our solution.
 
Selected choice: Same folder
 
Deployment option
 
Selecting Y will allow the app to be deployed instantly to all sites and be accessible everywhere.
 
Selected choice: N (install on each site explicitly)
 
Permissions to access web APIs
 
Choose if the components in the solution require permissions to access web APIs that are unique and not shared with other components in the tenant.
 
Selected choice: N (solution contains unique permissions)
 
Type of client-side component to create
 
We can choose to create a client-side web part or an extension. Choose web part option.
 
Selected choice: WebPart
 
Web part name
 
Hit Enter to select the default name or type in any other name.
 
Selected choice: PnpFileUpload
 
Web part description
 
Hit Enter to select the default description or type in any other value.
 
Framework to use
 
Select any JavaScript framework to develop the component. Available choices are - No JavaScript Framework, React, and Knockout.
Selected choice: React
 
Yeoman generator will perform a scaffolding process to generate the solution. The scaffolding process will take a significant amount of time.
 
Once the scaffolding process is completed, lock down the version of project dependencies by running the below command:
 
npm shrinkwrap
 
In the command prompt, type below command to open the solution in the code editor of your choice.
 
code .
 
NPM Packages Used,
 
On the command prompt, run below command.
 
npm i @pnp/logging @pnp/common @pnp/odata @pnp/sp --save
 
for Pollyfills
 
npm install --save @pnp/polyfill-ie11
 
in PnpFileUpload.tsx
  1. import "@pnp/polyfill-ie11";  
  2. import {sp} from '@pnp/sp';  
  3. export default class PnpFileUpload extends React.Component<IPnpFileUploadProps, {}> {  
  4.   constructor(props) {  
  5.     super(props);  
  6.     this.filesave = this.filesave.bind(this);  
  7.   }     
  8.   public render(): React.ReactElement<IPnpFileUploadProps> {  
  9.     return (  
  10.       <div>  
  11.         <div>  
  12.         <input type="file" name="myFile" id="newfile"></input></div>  
  13.         <div>  
  14.         <button onClick={this.filesave}>  
  15.         UploadFile  
  16.       </button></div>    
  17. </div>  
  18. );  
  19.   }  
  20.   private filesave(){  
  21.     let myfile = (document.querySelector("#newfile") as HTMLInputElement).files[0];  
  22.     if (myfile.size <= 10485760) {  
  23.       sp.web.getFolderByServerRelativeUrl("/sites/SPCapabilityTeam/MadhanNC/PnpLibrary").files.add(myfile.name, myfile, true).then(f => {  
  24.         console.log("File Uploaded");  
  25.         f.file.getItem().then(item => {  
  26.             item.update({  
  27.                 Title: "Metadata Updated"  
  28.             }).then((myupdate) => {  
  29.               console.log(myupdate);  
  30.               console.log("Metadata Updated");  
  31.             });  
  32.         });  
  33.     });    
  34.     }  
  35.     else{  
  36.       sp.web.getFolderByServerRelativeUrl("/sites/SPCapabilityTeam/MadhanNC/PnpLibrary")  
  37.       .files.addChunked(myfile.name, myfile)  
  38.       .then(({ file }) => file.getItem()).then((item:any) => {  
  39.         console.log("File Uploaded");  
  40.           return item.update({  
  41.               Title: 'Metadata Updated'  
  42.           }).then((myupdate) => {  
  43.             console.log(myupdate);  
  44.             console.log("Metadata Updated");  
  45.           });  
  46.       }).catch(console.log);  
  47. }}}  
Here, my list name is PnpLibrary and Title is metadata.
 
If we upload below 10mb, 1st method will hit. If above 10 mb, 2nd Method will hit.
 
Happy Coding :)