Introduction
This article explains how to upload multiple files into a SharePoint list in just a single click using Angular and jQuery.
Objective
Post multiple files into a SharePoint list in just one click.
Procedure
- Create a SharePoint library for storing files
- Create a web page for UI to upload files.
- Build respective HTML and script for the web page.
HTML Code
- <label class="btn btn-warning btn-xs" style="background-color: #6E2143;color: beige;" meetid="getFile" onclick="angular.element(this).scope().btnAgUpload(this)">
- <span class="glyphicon glyphicon-open"></span>Upload</label>
- <input type="file" id="getFile" multiple="multiple" onchange="angular.element(this).scope().uploadfiles(this)" style="display:none" />
Here we are hiding the input with style tag (display:none), but when we click on label, it triggers to the hidden input by onchange.
When onchange starts the trigger, it will call the actual upload function as shown in the script code.
Script Code
Label click
- $scope.btnAgUpload = function(val1){
- var fileEle = val1.attributes['meetid'].value;
- document.getElementById(fileEle).click();
- }
Hidden input calls the upload function.
- $scope.uploadfiles = function (ele){
- var agendaFile = $("#"+ele.attributes['id'].value);
- upload(agendaFile)
- }
Explanation for Upload functionality.
- function upload(sm) {
- var serverRelativeUrlToFolder = 'TestForMultipleFiles';
-
- var fileInput = sm;
- var fileCount = fileInput[0].files.length;
- var serverUrl = _spPageContextInfo.webAbsoluteUrl;
- var filesUploaded = 0;
- for(var i = 0; i < fileCount; i++){
-
- var getFile = getFileBuffer(i);
- getFile.done(function (arrayBuffer,i) {
-
- var addFile = addFileToFolder(arrayBuffer,i);
- addFile.done(function (file, status, xhr) {
- filesUploaded++;
- if(fileCount == filesUploaded){
- $("#getFile").val('');
- alert("All files uploaded successfully");
- filesUploaded = 0;
- }
- });
- addFile.fail(onError);
- });
- getFile.fail(onError);
-
- }
-
-
- function getFileBuffer(i) {
- var deferred = jQuery.Deferred();
- var reader = new FileReader();
- reader.onloadend = function (e) {
- deferred.resolve(e.target.result,i);
- }
- reader.onerror = function (e) {
- deferred.reject(e.target.error);
- }
- reader.readAsArrayBuffer(fileInput[0].files[i]);
- return deferred.promise();
- }
-
-
- function addFileToFolder(arrayBuffer,i) {
- var index = i;
-
-
- var fileName = fileInput[0].files[index].name;
- var tzoffset = (new Date()).getTimezoneOffset() * 60000;
- var localISOTime = (new Date(Date.now() - tzoffset)).toISOString().slice(0,-1).replace(":", "").slice(0,-2).replace(":", "").slice(0,-3).replace(".", "");
- CustomFilename = fileName.replace(".docx", ""+localISOTime+".docx");
-
- var fileCollectionEndpoint = String.format(
- "{0}/_api/web/getfolderbyserverrelativeurl('{1}')/files" +
- "/add(overwrite=true, url='{2}')",
- serverUrl, serverRelativeUrlToFolder, CustomFilename);
-
-
-
- return jQuery.ajax({
- url: fileCollectionEndpoint,
- type: "POST",
- data: arrayBuffer,
- processData: false,
- headers: {
- "accept": "application/json;odata=verbose",
- "X-RequestDigest": jQuery("#__REQUESTDIGEST").val()
- }
- });
- }
- }
-
-
- function onError(error) {
- alert(error.responseText);
- }
Let’s see the result on the screen as below.
Click on "Upload" and select multiple files.
Then, click on "Open". We get the success alert.
Go back to your list and check.
Finally, the selected files are uploaded successfully into SharePoint list.
Conclusion
Hope this will be the smart way to upload multiple files in just one click to SharePoint list using Angular and jQuery. And, avoid too many clicks to upload files.