I have been having an issue trying to upload files to an existing ASMX service that we have (see C# below). We currently have a Silverlight app which uses the service without any issues, but I have not been successful in getting our new app which uses AJAX to leverage the service.
I have pulled advice from these in for reference for much of this:
Have an of type="file" which triggers this function:
function ArrayBufferToBase64(buffer) {
var binary = '';
var bytes = new Uint8Array(buffer);
for (var xx = 0, len = bytes.byteLength; xx < len; ++xx) {
binary += String.fromCharCode(bytes[xx]);
}
return window.btoa(binary);
// return btoa(String.fromCharCode.apply(null, bytes)); // Note: tried this but always get an error "Maximum call stack size exceeded"
}
function DoUpload(files) {
var reader = new FileReader();
reader.onload = function (e) {
var data = {
file: ArrayBufferToBase64(e.target.result),
extension: file.name.substr(file.name.lastIndexOf("."))
};
$.ajax({
headers: {
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
url: "{baseurl}" + "FileInfo.asmx/UploadFile",
contentType: "application/json",
data: JSON.stringify(data),
dataType: 'json',
type: "POST",
success: function (response: ITempDocumentInfo) {
dfd.resolve();
},
error: function (e) {
console.error(e);
}
});
};
reader.readAsArrayBuffer(file);
}
The service lives in SharePoint and looks like this:
[WebService(Namespace = "http://www.blah.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class FileInfo : System.Web.Services.WebService
{
[WebMethod(EnableSession = true)]
public TempDocumentInfo AddScannedItem(string instanceId, string uri, string itemKey, string data, string extension)
{
byte[] bytes = new byte[data.Length * sizeof(char)];
System.Buffer.BlockCopy(data.ToCharArray(), 0, bytes, 0, bytes.Length);
var stream = new MemoryStream(bytes);
.
.
.
}
}
Once I have the data in C#, I cannot seem to be able to be able to do anything with it. For example. Uploading and processing a bmp image throws an exception:
var blah = System.Windows
.Media
.Imaging
.BmpBitmapDecoder
.Create(stream,
BitmapCreateOptions.PreservePixelFormat,
BitmapCacheOption.None);
Am I not generating the AJAX request correctly?