In previous article we saw how to get attachments of the list items using SharePoint 2013 REST services. Now in this article we will see how to upload the file as an attachment to the list item.
Following is the SharePoint 2013 REST service to add or upload attachments:
Following is the SharePoint 2013 REST service to add or upload attachments:
/_api/web/lists/getbytitle('testlist')/items(1)/AttachmentFiles/ add(FileName='" + fileName + "')
You just need to provide item id to which you want to attach file and attachment file name. Rest of the things are similar to how to upload the file.
Following is the complete code to upload the attachments:
<html>
<head>
<script src="/Site%20Assets/js/jquery-1.11.1.min.js"></script>
<script>
function AddAttachments()
{
var digest = "";
$.ajax(
{
url: "/_api/contextinfo",
method: "POST",
headers: {
"ACCEPT": "application/json;odata=verbose",
"content-type": "application/json;odata=verbose"
},
success: function (data) {
digest = data.d.GetContextWebInformation.FormDigestValue;
},
error: function (data) {
}
}).done(function() {
var fileInput = $('#uploadFile');
var fileName = fileInput[0].files[0].name;
var reader = new FileReader();
reader.onload = function (e) {
var fileData = e.target.result;
var res11 = $.ajax(
{
url: "/_api/web/lists/getbytitle('testlist')/items(1)/AttachmentFiles/ add(FileName='" + fileName + "')",
method: "POST",
binaryStringRequestBody: true,
data: fileData,
processData: false,
headers: {
"ACCEPT": "application/json;odata=verbose",
"X-RequestDigest": digest,
"content-length": fileData.byteLength
},
success: function (data) {
},
error: function (data) {
alert("Error occured." + data.responseText);
}
});
};
reader.readAsArrayBuffer(fileInput[0].files[0]);
});
}
</script>
</head>
<body>
<div>
<input id="uploadFile" type="file">
</div>
<div>
<input type="submit" onclick="AddAttachments()" value="Add Attachments"> </input>
</div>
</body>
</html>
<head>
<script src="/Site%20Assets/js/jquery-1.11.1.min.js"></script>
<script>
function AddAttachments()
{
var digest = "";
$.ajax(
{
url: "/_api/contextinfo",
method: "POST",
headers: {
"ACCEPT": "application/json;odata=verbose",
"content-type": "application/json;odata=verbose"
},
success: function (data) {
digest = data.d.GetContextWebInformation.FormDigestValue;
},
error: function (data) {
}
}).done(function() {
var fileInput = $('#uploadFile');
var fileName = fileInput[0].files[0].name;
var reader = new FileReader();
reader.onload = function (e) {
var fileData = e.target.result;
var res11 = $.ajax(
{
url: "/_api/web/lists/getbytitle('testlist')/items(1)/AttachmentFiles/ add(FileName='" + fileName + "')",
method: "POST",
binaryStringRequestBody: true,
data: fileData,
processData: false,
headers: {
"ACCEPT": "application/json;odata=verbose",
"X-RequestDigest": digest,
"content-length": fileData.byteLength
},
success: function (data) {
},
error: function (data) {
alert("Error occured." + data.responseText);
}
});
};
reader.readAsArrayBuffer(fileInput[0].files[0]);
});
}
</script>
</head>
<body>
<div>
<input id="uploadFile" type="file">
</div>
<div>
<input type="submit" onclick="AddAttachments()" value="Add Attachments"> </input>
</div>
</body>
</html>
No comments:
Post a Comment