public void LoadBinaryAsync(string path, Action<byte[]> success, Action<Exception> error)
{
var xhr = new XMLHttpRequest();
xhr.open("GET", path);
xhr.responseType = "arraybuffer";
xhr.onreadystatechange = e =>
{
if (xhr.readyState == 4)
{
if (xhr.status == 200)
{
var reader = NewUint8Array(xhr.response);
success(reader);
}
// Error handling
else if (xhr.status == 0)
{
error(new FileLoadException("You are offline!!\n Please Check Your Network."));
}
else if (xhr.status == 404)
{
error(new FileLoadException("Requested URL not found."));
}
else if (xhr.status == 500)
{
error(new FileLoadException("Internel Server Error."));
}
else if (xhr.statusText == "parsererror")
{
error(new FileLoadException("Error.\nParsing JSON Request failed."));
}
else if (xhr.statusText == "timeout")
{
error(new FileLoadException("Request Time out."));
}
else
{
error(new FileLoadException("Unknow Error: " + xhr.responseText));
}
}
};
xhr.open("GET", path, true);
xhr.responseType = "arraybuffer";
// IE fallback
if (xhr.responseType != "arraybuffer")
{
// use VB Loader to load binary array
dynamic vbArr = VbAjaxLoader("GET", path);
var fileContents = vbArr.toArray();
// decode byte array to string
var data = new StringBuilder();
var i = 0;
while (i < (fileContents.length - 1))
{
data.Append(((char)(fileContents[i])));
i++;
}
var reader = GetBytesFromString(data.ToString());
success(reader);
return;
}
xhr.send();
}