-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdummy.js
112 lines (104 loc) · 2.97 KB
/
dummy.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
$(document).ready(function() {
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function() {
init()
})
})
function init() {
$('#btnUploadFiles').click(function() {
var files = $('#inputTypeFiles')[0].files
uploadFiles(files[0]) // uploading singe file
})
}
function uploadFiles(uploadFileObj) {
var fileName = uploadFileObj.name
var webUrl = _spPageContextInfo.webAbsoluteUrl
var documentLibrary = 'SampleDocuments'
var folderName = 'Folder1'
var targetUrl = _spPageContextInfo.webServerRelativeUrl + '/' + documentLibrary + '/' + folderName
var url = webUrl + "/_api/Web/GetFolderByServerRelativeUrl(@target)/Files/add(overwrite=true, url='" + fileName + "')?$expand=ListItemAllFields&@target='" + targetUrl + "'"
uploadFileToFolder(
uploadFileObj,
url,
function(data) {
var file = data.d
var updateObject = {
__metadata: {
type: file.ListItemAllFields.__metadata.type
},
Column1: 'Test Data', //meta data column1
Column2: 'Test Data' //meta data column2
}
url = webUrl + "/_api/Web/lists/getbytitle('" + documentLibrary + "')/items(" + file.ListItemAllFields.Id + ')'
updateFileMetadata(
url,
updateObject,
file,
function(data) {
alert('File uploaded & meta data updation done successfully')
},
function(data) {
alert('File upload done but meta data updating FAILED')
}
)
},
function(data) {
alert('File uploading and meta data updating FAILED')
}
)
}
function getFileBuffer(uploadFile) {
var deferred = jQuery.Deferred()
var reader = new FileReader()
reader.onloadend = function(e) {
deferred.resolve(e.target.result)
}
reader.onerror = function(e) {
deferred.reject(e.target.error)
}
reader.readAsArrayBuffer(uploadFile)
return deferred.promise()
}
function uploadFileToFolder(fileObj, url, success, failure) {
var apiUrl = url
var getFile = getFileBuffer(fileObj)
getFile.done(function(arrayBuffer) {
$.ajax({
url: apiUrl,
type: 'POST',
data: arrayBuffer,
processData: false,
async: false,
headers: {
accept: 'application/json;odata=verbose',
'X-RequestDigest': jQuery('#__REQUESTDIGEST').val()
},
success: function(data) {
success(data)
},
error: function(data) {
failure(data)
}
})
})
}
function updateFileMetadata(apiUrl, updateObject, file, success, failure) {
$.ajax({
url: apiUrl,
type: 'POST',
async: false,
data: JSON.stringify(updateObject),
headers: {
accept: 'application/json;odata=verbose',
'X-RequestDigest': $('#__REQUESTDIGEST').val(),
'Content-Type': 'application/json;odata=verbose',
'X-Http-Method': 'MERGE',
'IF-MATCH': file.ListItemAllFields.__metadata.etag
},
success: function(data) {
success(data)
},
error: function(data) {
failure(data)
}
})
}