Today I came across a requirement to upload file to SharePoint Document library. This task was to be done using SPFx. Pnp helped me with this. Here is the code i used.
Below is the code for uploading file and meta data.
Here is the HTML for file upload control and button.
Below is the click event of Upload button.
Hope this Helps!
Thanks,
Keyur
Below is the code for uploading file and meta data.
private UploadFile(): void {
let input = <HTMLInputElement> document.getElementById("fileUpload");
let files = input.files;
for (let index = 0; index < files.length; index++) {
const file = files[index];
if (file.size<= 10485760)
{
//upload small file in document library
pnp.sp.web.getFolderByServerRelativeUrl("/sites/Dummy/ProcessDocuments/").files.
add(file.name, file, true).then(f=> {
f.file.getItem().then(item => {
item.update({ProcessInformationIDId: itemID}).then(f => {
console.log("File uploaded successfully");
});
});
})
} else { // upload large file in document library
pnp.sp.web.getFolderByServerRelativeUrl("/sites/Dummy/ProcessDocuments/")
.files.addChunked(file.name, file, data => {
console.log({data: data, message: "progress"});
}, true).then(f => {
console.log("File uploaded successfully");
});
}
}
}Here is the HTML for file upload control and button.
<input type="file" name="name" id="fileUpload" multiple><button id="btnUploadFiles"
class="btn btn-primary">Upload</button>
Below is the click event of Upload button.
$(document).on('click', '#btnUploadFiles', function (e) {
try { /*if (formState == "view" || formState == "edit") {
if (itemID != "" || itemID != null) {
if (processInfrmationDetails.TRR0RunwayWFStatus == null || processInfrmationDetails.TRR0RunwayWFStatus == Status.DraftInWork) {
this.ApproveTRR0Runway(Status.Approved, this);
}
}
}*/
this.UploadFile();
} catch (error) {
console.log(error);
}
}.bind(this));
Hope this Helps!
Thanks,
Keyur
Comments
Post a Comment