-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib_attachment.html
238 lines (217 loc) · 9.96 KB
/
lib_attachment.html
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<h2>Uploader</h2>
<div>
<h5>Choose files</h5>
<input type="file" value="+Add Files" id="AddFiles">
<br>
<h5>Enter the author</h5>
<input type="text" id="author">
<br>
<p></p>
<input type="button" value="Upload" id="Upload">
<p></p>
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>Filename</th>
<th>Unique id</th>
<th>Author</th>
</tr>
</thead>
<tbody id="result">
</tbody>
</table>
</div>
<!--script>
var listTitle = 'duallist1';
var itemId = 55;
var fileInput = document.getElementById("AddFiles");
var file = fileInput.files[0];
function uploadFile(folderUrl,fileName,fileContent,success,error)
{
var ctx = SP.ClientContext.get_current();
var folder = ctx.get_web().getFolderByServerRelativeUrl(folderUrl);
var encContent = new SP.Base64EncodedByteArray();
for (var b = 0; b < fileContent.length; b++) {
encContent.append(fileContent[b]);
}
var createInfo = new SP.FileCreationInformation();
createInfo.set_content(encContent);
createInfo.set_url(fileName);
folder.get_files().add(createInfo);
ctx.executeQueryAsync(
function(){
console.log('Attachment file has been uploaded');
},
function(sender,args){
console.log(args.get_message());
});
}
</script-->
<!--script>
//old code
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 + "')/rootfolder/files/add(FileName='" + filename + "')",
method: 'POST',
data: buffer,
processData: false,
headers: {
"Accept": "application/json; odata=verbose",
"content-type": "application/json; odata=verbose",
"X-RequestDigest": document.getElementById("__REQUESTDIGEST").value
},
success: function (data) {
alert("File uploaded successfully!");
console.log("File uploaded successfully!");
},
failure: function (data) {
alert("Error in uploading file!");
console.log("Error" + data.responseJSON.error);
}
});
});
</script-->
<script>
$(document).ready(function () {
function guid() {
return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
}
function s4() {
return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
}
var inputID = guid();
$("#Upload").click(function () {
var author = $("#author").val();
var listname = "lib_attachment";
var file = $("#AddFiles")[0].files[0];
var parts = $("#AddFiles")[0].value.split("\\");
var fileName = parts[parts.length - 1];
var reader = new FileReader();
reader.onload = function (e) {
addItem(e.target.result, fileName);
}
reader.onerror = function (e) {
alert(e.target.error);
}
reader.readAsArrayBuffer(file);
function addItem(buffer, fileName) {
var call = uploadDocument(buffer, fileName);
call.done(function (data, textStatus, jqXHR) {
var call2 = getItem(data.d);
call2.done(function (data, textStatus, jqXHR) {
var item = data.d;
var call3 = updateItemFields(item);
call3.done(function (data, textStatus, jqXHR) {
$("#result").append("<tr id='"+fileName+"'><td>" + fileName + "</td><td>" + inputID + "</td><td>" + author + "</td><td><input type='button' id='delete' value='Delete' onclick='deleteItem("+fileName+")'></td></tr>");
});
call3.fail(function (jqXHR, textStatus, errorThrown) {
failHandler(jqXHR, textStatus, errorThrown);
});
});
call2.fail(function (jqXHR, textStatus, errorThrown) {
failHandler(jqXHR, textStatus, errorThrown);
});
});
call.fail(function (jqXHR, textStatus, errorThrown) {
failHandler(jqXHR, textStatus, errorThrown);
});
}
function uploadDocument(buffer, fileName) {
var url = String.format(
"{0}/_api/Web/Lists/getByTitle('" + listname + "')/RootFolder/Files/Add(url='{1}', overwrite=true)",
_spPageContextInfo.webAbsoluteUrl, fileName);
var call = $.ajax({
url: url,
type: "POST",
data: buffer,
processData: false,
headers: {
Accept: "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"Content-Length": buffer.byteLength
}
});
return call;
}
function getItem(file) {
var call = $.ajax({
url: file.ListItemAllFields.__deferred.uri,
type: "GET",
dataType: "json",
headers: {
Accept: "application/json;odata=verbose"
}
});
return call;
}
function updateItemFields(item) {
var call = $.ajax({
url: _spPageContextInfo.webAbsoluteUrl +
"/_api/Web/Lists/getByTitle('" + listname + "')/Items(" +
item.Id + ")",
type: "POST",
data: JSON.stringify({
"__metadata": { type: "SP.Data.Lib_x005f_attachmentItem" },
author0: author,
uniqueid0: inputID
}),
headers: {
Accept: "application/json;odata=verbose",
"Content-Type": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"IF-MATCH": item.__metadata.etag,
"X-Http-Method": "MERGE"
}
});
return call;
}
function failHandler(jqXHR, textStatus, errorThrown) {
var response = JSON.parse(jqXHR.responseText);
var message = response ? response.error.message.value : textStatus;
alert("Call failed. Error: " + message);
}
function deleteItem(fileName) {
$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/getfilebyserverrelativeurl('" + _spPageContextInfo.webServerRelativeUrl + "/" + listname + "/" + fileName + "')",
type: "GET",
headers: {
"accept": "application/json;odata=verbose",
"X-HTTP-Method": "DELETE"
},
success: function (data) {
$("#"+fileName+"'").remove();
},
error: function (data) {
alert(data.responseJSON.error);
}
});
}
});
});
</script>
</body>
</html>