AlphaTab.Platform.JavaScript.JsFileLoader.LoadBinaryAsync C# (CSharp) Method

LoadBinaryAsync() public method

public LoadBinaryAsync ( string path, Action success, Action error ) : void
path string
success Action
error Action
return void
        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();
        }

Usage Example

Beispiel #1
0
        public override void Load(object data)
        {
            Element.className += " loading";

            if (JsTypeOf(data) == JsTypes.@string)
            {
                var fileLoader = new JsFileLoader();
                fileLoader.LoadBinaryAsync((string)data, b =>
                {
                    Renderer.As <WorkerScoreRenderer>().Load(b, TrackIndexes);
                }, e =>
                {
                    console.error(e);
                });
            }
            else
            {
                Renderer.As <WorkerScoreRenderer>().Load(data, TrackIndexes);
            }
        }
All Usage Examples Of AlphaTab.Platform.JavaScript.JsFileLoader::LoadBinaryAsync