internal static Task <ArrayBuffer> ReadBytesAsync(string path) { var tcs = new TaskCompletionSource <ArrayBuffer>(); if (Script.IsNode) { var fs = Script.Write <dynamic>(@"require(""fs"")"); fs.readFile(path, new Action <object, ArrayBuffer>((err, data) => { if (err != null) { throw new IOException(); } tcs.SetResult(data); })); } else { var req = new XMLHttpRequest(); req.Open("GET", path, true); req.OverrideMimeType("text/plain; charset=binary-data"); req.Send(null); req.OnReadyStateChange = () => { if (req.ReadyState != 4) { return; } if (req.Status != 200) { throw new IOException($"Status of request to {path} returned status: {req.Status}"); } string text = req.ResponseText; var resultArray = new Uint8Array(text.Length); text.ToCharArray().ForEach((v, index, array) => resultArray[index] = (byte)(v & byte.MaxValue)); tcs.SetResult(resultArray.Buffer); }; } return(tcs.Task); }
private string loadTextFileAjaxSync(string filePath, string mimeType) { var xmlhttp = new XMLHttpRequest(); xmlhttp.Open("GET", filePath, false); if (mimeType != null) { xmlhttp.OverrideMimeType(mimeType); } xmlhttp.Send(); if (xmlhttp.Status == 200) { return(xmlhttp.ResponseText); } else { // TODO Throw exception return(null); } }
private void ReadLayerInfos() { XMLHttpRequest xmlRequest = new XMLHttpRequest(); xmlRequest.OnReadyStateChange += () => { if (xmlRequest.ReadyState == AjaxReadyState.Done && xmlRequest.Status == 200) { List <LayerInfo> layerInfos = new List <LayerInfo>(); HTMLCollection root = xmlRequest.ResponseXML.GetElementsByTagName("LayerInfo"); HTMLCollection elements = xmlRequest.ResponseXML.GetElementsByTagName("Layer"); int i = 0; foreach (var child in elements) { if (child == null) { break; } int dimensionX = int.Parse(child.GetAttribute("DimensionX")); int dimensionY = int.Parse(child.GetAttribute("DimensionY")); layerInfos.Add(new LayerInfo(i, dimensionX, dimensionY)); ++i; } MapManager.Settings mapSettings; mapSettings.ImagePrefab = new WebMapTileImage(); mapSettings.LayerInfos = layerInfos; mapSettings.TileSize = int.Parse(root[0].GetAttribute("TileSize")); Map = new MapManager(mapSettings, AddToCanvas, RemoveFromCanvas, DrawOnCanvas); } }; string docUrl = Document.URL; docUrl = docUrl.Substring(0, docUrl.LastIndexOf('/')); xmlRequest.Open("GET", docUrl + "/img/LayerInfo.xml", false); xmlRequest.OverrideMimeType("text/xml"); xmlRequest.Send(); }
internal static ArrayBuffer ReadBytes(string path) { if (Script.IsNode) { var fs = Script.Write <dynamic>(@"require(""fs"")"); return((ArrayBuffer)fs.readFileSync(path)); } else { var req = new XMLHttpRequest(); req.Open("GET", path, false); req.OverrideMimeType("text/plain; charset=binary-data"); req.Send(null); if (req.Status != 200) { throw new IOException($"Status of request to {path} returned status: {req.Status}"); } string text = req.ResponseText; var resultArray = new Uint8Array(text.Length); text.ToCharArray().ForEach((v, index, array) => resultArray[index] = (byte)(v & byte.MaxValue)); return(resultArray.Buffer); } }