I'm trying to upload a file to a document library using the FileReader object, but failing to do so, since the files are uploaded in the wrong encoding.
This is code
$("#file").kendoUpload({
async: {
saveUrl: "save",
autoUpload: true
},
upload: function (e) {
$.each(e.files, function () {
var file = this.rawFile;
var reader = new FileReader();
reader.onload = function (e) {
var data = reader.result;
var soapEnv =
"<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='ht
<soap:Body>\
<CopyIntoItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'>\
<SourceUrl>" + file.name + "</SourceUrl>\
<DestinationUrls>\
<string>"+GetHostname() + folder + "/" + file.name + "</string>\
</DestinationUrls>\
<Fields>\
<FieldInformation Type='Text' DisplayName='Title' InternalName='Title' Value='"+ file.name + "' />\
</Fields>\
<Stream>" +window.btoa(data) + "</Stream>\
</CopyIntoItems>\
</soap:Body>\
</soap:Envelope>";
$.ajax({
url: "/documents/_vti_bin/copy.asmx",
beforeSend: function (xhr) { xhr.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/sharepoint/soap/CopyIntoItems"); },
type: "POST",
dataType: "xml",
data: soapEnv,
contentType: "text/xml; charset=\"utf-8\""
}).then(function (data, status) {
var itemUrl = xmlToJSON(data).Envelope.Body.CopyIntoItemsResponse.Results.CopyResult._DestinationUrl;
return documentsUtilities.checkIn(itemUrl);
})
.then(function(res){
//notifiy user and automatically reload the grid
});
};
});
e.preventDefault();
}
The problem im facing is located in the soap - string itself.
Passing around that data as <Stream>" +window.btoa(data) + "</Stream>
results in the file being uploaded with its content as base64 encoded. Images are black and text files are unreadable.
Passing the data object itself with <Stream>" + data + "</Stream>\
results in a bad request.
What is the correct way to upload a file into document library on SharePoint 2010 using the FileReader API?