I have code I use to upload files using the filereader object. The uploadFile function is called on the event change of a file input and passes the event.target.files to the function. If you are using IE you will need to use a polyfill to provide the same functionality. I have used https://github.com/Jahdrien/FileReader.
Event Object:
jQuery('#' + element.id).fileReader({
id: 'fileReaderSWFObject',
filereader: '/Site/SiteAssets/JS/lib/filereader.swf',
expressInstall: '/Site/SiteAssets/JS/lib/expressInstall.swf',
debugMode: true
});
jQuery('#' + element.id).on('change', function (event) {
console.log('fileChange');
valueAccessor().uploadFile(event.target.files);
});
File Upload Function
self.uploadFile = function (files) {
filereader = {},
file = {},
file = files[0];
filereader = new FileReader();
filereader.fi
lename = file.name;
self.uploadFileName(file.name);
self.uploadStatus('Reading File');
filereader.onload = function () {
console.log('onload');
var data = filereader.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);
self.uploadStatus('Uploading File');
jQuery.when(uploadFile(filereader.filename, data)).done(function(results)
{
self.uploadStatus('Upload Complete');
});
}
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);
}
Web Service Call
function uploadFile(FileName,FileData) {
//FileData is a byte array
console.log("uploading file");
var url= "https://"+location.hostname+"/SiteName/FolderName/"+FileName;
var soapEnv =
"<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'> \
<soap:Body>\
<CopyIntoItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'>\
<SourceUrl>https://"+location.hostname+"/SiteName/FolerName/ </SourceUrl>\
<DestinationUrls>\
<string> "+ url + "</string>\
</DestinationUrls>\
<Fields>\
<FieldInformation Type='Text' DisplayName='Title' InternalName='Title' Value='Test' />\
</Fields>\
<Stream>"+ FileData +"</Stream>\
</CopyIntoItems>\
</soap:Body>\
</soap:Envelope>";
return jQuery.ajax({
url: "https://"+location.hostname+"/SiteName/_vti_bin/copy.asmx",
beforeSend: function (xhr) { xhr.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/sharepoint/soap/CopyIntoItems"); },
type: "POST",
dataType: "xml",
data: soapEnv,
complete: processResult,
contentType: "text/xml; charset=\"utf-8\""
}).promise();
}