In my current project I have encountered an interesting problem. I am trying to attach multiple files (pdf's, doc's etc. not only text files) to my SharePoint List, getting them from a document library. Problem is, when I use methods like get_web(), get_lists() etc. ending at get_file() from my current SP.ListItem, the type of file is SP.File (which is pretty obvious). In search for a solution I found this kind of method that uses SPServices:
http://www.bjw.co.nz/developer/sharepoint-2010/1188-spservices-addattachment-jquery-example
Here is the code:
function handleFileChange(listName,itemId,files){
var filereader = {},
file = {},
i=0;
//loop over each file selected
for(i = 0; i < files.length; i++) {
file = files[i];
filereader = new FileReader();
filereader.filename = file.name;
filereader.onload = function() {
var data = this.result,
n=data.indexOf(";base64,") + 8;
//removing the first part of the dataurl give us the base64 bytes we need to feed to sharepoint
data= data.substring(n);
$().SPServices({
operation: "AddAttachment",
listName: listName,
asynch: false,
listItemID:itemId,
fileName: this.filename,
attachment: data,
completefunc: function (xData, Status) {
console.log('attachment upload complete',xData,status);
}
});
};
filereader.onabort = function() {
alert("The upload was aborted.");
};
filereader.onerror = function() {
alert("An error occured while reading the file.");
};
//fire the onload function giving it the dataurl
filereader.readAsDataURL(file);
}
};
To use this solution I need to have an object of type File, not SP.File. My question is, how can I get the actual file from a SharePoint document library to complete my task? I couldn't find an answer anywhere, so even the smallest hint will be appreciated.