You are on page 1of 3

<!

DOCTYPE html>
<html>
<head>
<title></title>
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.9.1.min.js"
type="text/javascript"></script>
<script type="text/javascript">
var context;
var web;
var Url;
var listItemEntity;
$(document).ready(function () {
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', FunctionReady);
ItemSubmit();
});

function FunctionReady() {
context = new SP.ClientContext.get_current();
web = context.get_web();
context.load(web);
context.executeQueryAsync(onRequestSuccess, onRequestFailure);
}

function onRequestSuccess() {
Url = web.get_url();
GetListItemEntity(Url);
}

function onRequestFailure(sender, args) {
alert("Error Occured:" + args.get_message());
}

//Used to get the List Item Entity Type
function GetListItemEntity(webUrl) {
var queryUrl = webUrl + "/_api/lists/getbytitle('TestList')?
$select=ListItemEntityTypeFullName";
$.ajax({
url: queryUrl,
method: "GET",
headers: { "Accept": "application/json;odata=verbose" },
success: onEntitySuccess,
error: onEntityFailure
});
}
function onEntitySuccess(data) {
listItemEntity = data.d.ListItemEntityTypeFullName;
}
function onEntityFailure(err) {
alert(err.statusText);
}

function ItemSubmit() {
//Button click Event to upload files to List Item
$("#btnSubmit").click(function () {
//checking Whether document is uploaded or not - Validation
if (document.getElementById("inputFile").files.length === 0) {
alert("Select a file..and click on submit");
return;
}

//TestList . var deferred = $. var filename = parts[parts. else { //Reading the Uploadef file values var parts = document. var queryUrl = Url + "/_api/lists/GetByTitle('" + listName + "')/items(" + id + ")/AttachmentFiles/add(FileName='" + file.split("\\").byteLength }.1]. var content = new SP. function (err) { deferred.odata=verbose"." + error. alert(file). uploadFileSP("TestList". alert(id).File Name of the attachment //file . "X-RequestDigest": $("#__REQUESTDIGEST").name + "')".is our List Item ID //filename . file) { alert('hi'). }). alert(file). } function uploadFileSP(listName.status + ". } function getFileBuffer(file) { .getElementById("inputFile"). } }). alert(fileName). "1".is our Listname //1 . type: "POST".getElementById("inputFile"). success: onQuerySucess.promise(). file).then( function (buffer) { var bytes = new Uint8Array(buffer).file data alert(filename). return deferred.reject(err). }. processData: false.Base64EncodedByteArray().odata=verbose". data: buffer.length . } function onQuerySucess() { alert('Attachement Added Successfully'). headers: { "accept": "application/json. error: onQueryFailure }).val(). $. } function onQueryFailure(error) { alert("Failure:" + error. fileName.statusText). var file = document. id. filename.ajax({ url: queryUrl.files[0]. getFileBuffer(file).value. "content-length": buffer.Deferred(). contentType: "application/json.

onerror = function (e) { deferred.reject(e.error).target. } reader. } reader.target. } </script> </head> <body> <input type="file" id="inputFile" /> <input type="button" id="btnSubmit" value="Submit"> </body> </html> . reader.readAsArrayBuffer(file).onload = function (e) { deferred. alert("In getFileBuffer").result).promise(). return deferred.Deferred(). var deferred = jQuery. var reader = new FileReader().resolve(e.