Shiv Sharma

Shiv Sharma

  • 238
  • 7.1k
  • 571.2k

Files not uploading successfully

Feb 13 2019 11:35 PM

Hi All,

If i uploaded big file in Secondary and small file in primary its upload successfully and till not upload both it wait.

But if Primary control file is big and secondary control file is small then its not upload primary one

Window reload is calling wrong place (Primary is mandatory and secondary is not)

Below code :-

var controle1="N";
var controle2="N";
function onSuccess(){
// alert("in sucess");
// alert('Item created: 100' + newItem.get_id());
ItemID=newItem.get_id();
var elementId1=$("#PERSONAL0id").val();
var elementId2=$("#PERSONAL1id").val();
if(elementId1!="")
{
controle1="Y";
}
if(elementId2!="")
{
controle2="Y";
}
if(controle1=="Y")
{
var Control="P";
var Flag1="1";
uploadMFBySearch(ItemID,Control,Flag1);
}
if(controle2=="Y")
{
var Control="S";
var Flag1="2";
if(document.getElementById("PERSONALSid").files.length > 0){
uploadMFBySearch(ItemID,Control,Flag1);
}
}

function uploadMFBySearch(ItemID,Control,Flag1) {

//debugger;
//alert(ItemID);
var serverRelativeUrlToFolder = 'docLibrary/';
var fileInput = jQuery('#PERSONAL'+Control+'id');
var newName ="mulDoc";
var docType="muldocument";
var filerename="muldocument";
var fileCount = fileInput[0].files.length;
var serverUrl = _spPageContextInfo.webAbsoluteUrl;
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 = getFileBufferMF(i);
getFile.done(function (arrayBuffer, i) { // Add the file to the SharePoint folder.
var addFile = addFileToFolderMF(arrayBuffer, i,ItemID);
addFile.done(function (file, status, xhr) {//Get ID of File uploaded //mFclear();
var getfileID =getItem(file.d.ListItemAllFields.__deferred.uri);//var getfileID =getItem(file.d); //getListItem(file.d.ListItemAllFields.__deferred.uri);//getItem(file.d);
getfileID.done(function (fResult) {
var colObjectMF = new Object();
colObjectMF["DocumentCategory"] = "yo";//var changeItem = updateFileMetadataMF(libraryName, fResult.d, colObject);
var changeItem = updateFileMetadataMF(fResult.d.__metadata,docType,newName,filerename,i,fileInput,ItemID,Control,Flag1);
// alert(newName);
changeItem.done(function (result) {
filesUploaded++;
//alert("HI");
var ReqFormat=100000;
var NewReqNo=ReqFormat+ItemID;
if (fileCount == filesUploaded) {
//alert("All files uploaded successfully");
filesUploaded = 0;
if( document.getElementById("PERSONALSid").files.length == 0){

SavePeople();
$('#pageLoader').hide();
alert("Your request has been successfully submitted! Request No:"+NewReqNo);
window.location.href = "https://ABC.com/sites/MyPortal.aspx";

}
else {
//alert("Before Save People"+DepartmentNew);


SavePeople();
//alert(Flag1);
$('#pageLoader').hide();
if(Flag1=="2"){
alert("Your request has been successfully submitted! Request No:"+NewReqNo);
window.location.href = "https://ABC.com/sites/MyPortal.aspx";
}
}



}
});
changeItem.fail(function (result) {
$('#pageLoader').hide();
alert("An error has occured while submitting request.");
});
}, function () { });
});
addFile.fail(onErrorMF);
});
getFile.fail(onErrorMF);
}




//This function will return the id of item uploaded.
function getItem(fileListItemUri) {
return jQuery.ajax({
url: fileListItemUri,
type: "GET",
headers: { "accept": "application/json;odata=verbose" }
});
}




function getFileBufferMF(i) {// Get the local file as an array buffer.
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 addFileToFolderMF(arrayBuffer, i,ItemID) { // Add the file to the file collection in the Shared Documents folder.
var index = i;
fileNameMF1 = fileInput[0].files[index].name;
var ReqFormat=100000;
fileNameMF2=ReqFormat+ItemID+fileNameMF1;
var fileCollectionEndpoint = String.format("{0}/_api/web/getfolderbyserverrelativeurl('{1}')/files/add(overwrite=true, url='{2}')",serverUrl, serverRelativeUrlToFolder, fileNameMF2);
return jQuery.ajax({
url: fileCollectionEndpoint,
type: "POST",
data: arrayBuffer,
//async:false,
processData: false,
headers: {
"accept": "application/json;odata=verbose",
"X-RequestDigest": jQuery("#__REQUESTDIGEST").val(),
"content-length": arrayBuffer.byteLength
}
});

}
}


function onErrorMF(error) {
console.log("mf:"+error.responseText);
//alert("mf:"+error.responseText);
}


function updateFileMetadataMF(item,docType,newName,filerename,i,fileInput,ItemID,Control) {


var ReqFormat=100000;
ItemID1=ReqFormat+ItemID;
var body = String.format("{{'__metadata':{{'type':'{0}'}},'Title':'{1}','DocumentType':'{2}'}}",item.type,ItemID1,Control);
return jQuery.ajax({
url: item.uri,
type: "POST",
//async:false,
data: body,
headers: {
"X-RequestDigest": jQuery("#__REQUESTDIGEST").val(),
"content-type": "application/json;odata=verbose",
"content-length": body.length,
"IF-MATCH": item.etag,
"X-HTTP-Method": "MERGE"
}
});
return dfd.promise();


}

---------------------------Attchment control -----------------------------

<div class="col-md-2">
<label for="countries">Primary documents:</label> <span style="color:#de0606;">*</span>
</div>
<div class="col-md-3 files" id="attachFilesHolder">
<!--<input id="file_input" type="file" name="files[]">-->
<input type="file" id="PERSONALPid" class="filestyle" data-icon="" >
</div>


<div class="col-md-2">
<label for="countries">Secondary documents:</label>
</div>
<div class="col-md-3 files" id="attachFilesHolder2">
<!--<input id="file_input" type="file" name="files[]">-->
<input type="file" id="PERSONALSid" class="filestyle" data-icon="" multiple="multiple">
</div>

 

Its issue of page load window.location.href

As its after upload of any one of them window.location.href will called, so need to call in that way it will call accordingly.

Can you please go through this code and check primary big 7-10 mb file and secondary KB file and see it will not upload primary file as it load as upload secondary file.

 

Answers (2)