Uploading Multiple documents into a SharePoint document library using Javascript
<script src="https://code.jquery.com/jquery-3.2.1.js" type="text\javascript"></script>
<input type="file" id="inputFile" />
<script type="text\javascript">
var DocumentlistName="YourDocumentLibraryName";
var serverUrl = _spPageContextInfo.webAbsoluteUrl;
function uploadFile() {
debugger;
// Define the folder path for this example.
// Get test values from the file input and text input page controls.
var fileInput = $('#inputFile');
var newName = $('#displayName').val();
fileCount = fileInput[0].files.length;
// Get the server URL.
var filesUploaded = 0;
for(var i = 0; i < fileCount; i++){
// Initiate method calls using jQuery promises.
// Get the local file as an array buffer.
var getFile = getFileBuffer(i);
getFile.done(function (arrayBuffer,i) {
// Add the file to the SharePoint folder.
var addFile = addFileToFolder(arrayBuffer,i);
addFile.done(function (file, status, xhr) {
//$("#msg").append("<div>File : "+file.d.Name+" ... uploaded sucessfully</div>");
filesUploaded++;
if(fileCount == filesUploaded){
alert("All files uploaded successfully");
//$("#msg").append("<div>All files uploaded successfully</div>");
GetListItems(getListItemssuccess,getListItemsfailure);
$("#inputFile").value = null;
filesUploaded = 0;
}
});
addFile.fail(onError);
});
getFile.fail(onError);
}
// Get the local file as an array buffer.
function getFileBuffer(i) {
debugger;
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();
}
// Add the file to the file collection in the Shared Documents folder.
function addFileToFolder(arrayBuffer,i) {
debugger;
var index = i;
// Get the file name from the file input control on the page.
var fileName = fileInput[0].files[index].name;
// Construct the endpoint.
var fileCollectionEndpoint = String.format(
"{0}/_api/web/getfolderbyserverrelativeurl('{1}')/files" +
"/add(overwrite=true, url='{2}')",
serverUrl, DocumentlistName, fileName);
// Send the request and return the response.
// This call returns the SharePoint file.
return jQuery.ajax({
url: fileCollectionEndpoint,
type: "POST",
data: arrayBuffer,
processData: false,
headers: {
"accept": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"content-length": arrayBuffer.byteLength
}
});
}
}
// Display error messages.
function onError(error) {
alert(error.responseText);
}
</script>
<script src="https://code.jquery.com/jquery-3.2.1.js" type="text\javascript"></script>
<input type="file" id="inputFile" />
<script type="text\javascript">
var DocumentlistName="YourDocumentLibraryName";
var serverUrl = _spPageContextInfo.webAbsoluteUrl;
function uploadFile() {
debugger;
// Define the folder path for this example.
// Get test values from the file input and text input page controls.
var fileInput = $('#inputFile');
var newName = $('#displayName').val();
fileCount = fileInput[0].files.length;
// Get the server URL.
var filesUploaded = 0;
for(var i = 0; i < fileCount; i++){
// Initiate method calls using jQuery promises.
// Get the local file as an array buffer.
var getFile = getFileBuffer(i);
getFile.done(function (arrayBuffer,i) {
// Add the file to the SharePoint folder.
var addFile = addFileToFolder(arrayBuffer,i);
addFile.done(function (file, status, xhr) {
//$("#msg").append("<div>File : "+file.d.Name+" ... uploaded sucessfully</div>");
filesUploaded++;
if(fileCount == filesUploaded){
alert("All files uploaded successfully");
//$("#msg").append("<div>All files uploaded successfully</div>");
GetListItems(getListItemssuccess,getListItemsfailure);
$("#inputFile").value = null;
filesUploaded = 0;
}
});
addFile.fail(onError);
});
getFile.fail(onError);
}
// Get the local file as an array buffer.
function getFileBuffer(i) {
debugger;
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();
}
// Add the file to the file collection in the Shared Documents folder.
function addFileToFolder(arrayBuffer,i) {
debugger;
var index = i;
// Get the file name from the file input control on the page.
var fileName = fileInput[0].files[index].name;
// Construct the endpoint.
var fileCollectionEndpoint = String.format(
"{0}/_api/web/getfolderbyserverrelativeurl('{1}')/files" +
"/add(overwrite=true, url='{2}')",
serverUrl, DocumentlistName, fileName);
// Send the request and return the response.
// This call returns the SharePoint file.
return jQuery.ajax({
url: fileCollectionEndpoint,
type: "POST",
data: arrayBuffer,
processData: false,
headers: {
"accept": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"content-length": arrayBuffer.byteLength
}
});
}
}
// Display error messages.
function onError(error) {
alert(error.responseText);
}
</script>
No comments:
Post a Comment