var currentUser;
$(document).ready(function(){
GetUserDetails();
$("#designFile").change(function() {
file = $(this)[0].files[0];
fileArray.push(file);
});
$("#storyFile").change(function() {
file = $(this)[0].files[0];
fileArray.push(file);
});
});
function GetUserDetails() {
var url = _spPageContextInfo.webAbsoluteUrl + "/_api/web/currentuser";
$.ajax({
url: url,
headers: {
Accept: "application/json;odata=verbose"
},
async: false,
success: function (data) {
var items = data.d.results; // Data will have user object
GetUser(data);
},
eror: function (data) {
alert("An error occurred. Please try again.");
}
});
}
function GetUser(data)
{
var userName = data.d["Title"];
var Email = data.d["Email"];
$('#UserName').val(userName);
$('#Email').val(Email );
$('#UserName').attr('disabled','disabled');
$('#Email').attr('disabled','disabled');
}
function SaveValues()
{
var userName = $('#UserName').val();
var email = $('#Email').val();
var desc= $('#desc').val();
var userId = _spPageContextInfo.userId;
var listName = "CE Dashboard Design Contest Page";
var itemProperties = {
"__metadata": { "type": "SP.Data.CE_x0020_Dashboard_x0020_Design_x0020_Contest_x0020_PageListItem" },
"Email": email,
"Description":desc,
"NameId":userId
};
$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('" + listName + "')/items",
type: "POST",
contentType: "application/json;odata=verbose",
data: JSON.stringify(itemProperties),
headers: {
"Accept": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
success: function (data) {
console.log(JSON.stringify(data.d));
UploadFiles(listName,data.d.ID);
},
error: function (data) {
console.log(data.responseText);
}
});
}
var ID = 1; var fileArray = [];
var listname = "UploadTEST";
function UploadFiles(listName,itemId)
{
if (fileArray.length != 0)
{
for (i = 0; i < fileArray.length; i++){
uploadFile(listName,itemId, fileArray[i])
}
alert('Your files submitted sucessfully !!');
$('#desc').val('');
location.reload();
}
}
function uploadFile(listname, ID, file) {
var getFileBuffer = function (file) {
var deferred = $.Deferred();
var reader = new FileReader();
reader.onload = function (e) {
deferred.resolve(e.target.result);
}
reader.onerror = function (e) {
deferred.reject(e.target.error);
}
reader.readAsArrayBuffer(file);
return deferred.promise();
};
getFileBuffer(file).then(function (buffer) {
$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('" + listname + "')/items(" + ID + ")/AttachmentFiles/add(FileName='" + file.name + "')",
method: 'POST',
async: false,
data: buffer,
processData: false,
headers: {
"Accept": "application/json; odata=verbose",
"content-type": "application/json; odata=verbose",
"X-RequestDigest": document.getElementById("__REQUESTDIGEST").value
}, success: onAttachmentSucess(file.name)
});
});
function onAttachmentSucess(name) {
}
}
No comments:
Post a Comment