private static async Task DownloadData(PortalItem item) { var dataDir = GetDataFolder(); var dataFile = Path.Combine(dataDir, item.Name); // Create the directory if needed if (!Directory.Exists(dataDir)) { Directory.CreateDirectory(dataDir); } // Download the data using (var stream = await item.GetDataAsync()) using (var output = File.Create(dataFile)) await stream.CopyToAsync(output); // Uzip the data if needed if (Path.GetExtension(dataFile) == ".zip") { await UnpackData(dataFile, dataDir); } // Update __sample.config to save the last time downloaded var configFilePath = Path.Combine(dataDir, "__sample.config"); #if WINDOWS_UWP await File.WriteAllTextAsync(configFilePath, $"Data downloaded: {DateTime.Now}"); #else File.WriteAllText(configFilePath, $"Data downloaded: {DateTime.Now}"); #endif }
//´´½¨web Scene public async void InitScene(PortalItem webSceneItem, SceneView sceneView) { try { System.IO.Stream st = await webSceneItem.GetDataAsync(); System.IO.StreamReader reader = new StreamReader(st); st.Position = 0; string strRes = reader.ReadToEnd(); reader.Close(); st.Close(); InitScene(strRes, sceneView); } catch { } }
/// <summary> /// Downloads a portal item and leaves a marker to track download date. /// </summary> /// <param name="item">Portal item to download.</param> /// <param name="cancellationToken">Cancellation token</param> private static async Task DownloadItem(PortalItem item, CancellationToken cancellationToken) { // Get sample data directory. string dataDir = Path.Combine(GetDataFolder(), item.ItemId); // Create directory matching item id. if (!Directory.Exists(dataDir)) { Directory.CreateDirectory(dataDir); } // Get the download task. Task <Stream> downloadTask = item.GetDataAsync(cancellationToken); // Get the path to the destination file. string tempFile = Path.Combine(dataDir, item.Name); // Download the file. using (var s = await downloadTask.ConfigureAwait(false)) { using (var f = File.Create(tempFile)) { await s.CopyToAsync(f).WithCancellation(cancellationToken).ConfigureAwait(false); } } // Unzip the file if it is a zip archive. if (tempFile.EndsWith(".zip")) { await UnpackData(tempFile, dataDir, cancellationToken); } // Write the __sample.config file. This is used to ensure that cached data did not go out-of-date. string configFilePath = Path.Combine(dataDir, "__sample.config"); File.WriteAllText(configFilePath, @"Data downloaded: " + DateTime.Now); }
private async void GetItemDataButton_Click(object sender, RoutedEventArgs e) { PortalItem portalItem = null; try { // Get Portal connection parameters var itemId = ItemIdBox.Text; var portalUrl = PortalUrlBox.Text; // Make sure Portal URL and item ID are specified if (string.IsNullOrEmpty(portalUrl)) { ItemDataBox.Text = "Portal URL must be specified"; return; } if (string.IsNullOrEmpty(itemId)) { ItemDataBox.Text = "Portal Item ID must be specified"; return; } // Update status text to indicate operation in progress StatusText.Text = "Retrieving item data..."; // Get the item and corresponding data for the specified Portal and item ID portalItem = await GetPortalItemAsync(portalUrl, itemId, UserNameBox.Text, PasswordBox.Password); var itemData = await portalItem.GetDataAsync(); // Convert the item data from a stream into a string var itemDataString = string.Empty; using (var reader = new StreamReader(itemData)) { itemDataString = await reader.ReadToEndAsync(); } try { // Check whether the content is JSON by attempting to convert it into a JSON object var itemDataJsonVal = JValue.Parse(itemDataString); // Format the JSON and display it var itemDataFormattedJson = itemDataJsonVal.ToString(Formatting.Indented); ItemDataBox.Text = itemDataFormattedJson; } catch { // Something went wrong when parsing or formatting the JSON, so just display the item content as-is ItemDataBox.Text = itemDataString; } } catch (Exception ex) { // An unexpected error occurred - notify user StatusText.Text = "Error retrieving data"; ItemDataBox.Text = $"Error while attempting to retrieve item data: {ex.Message}\n\nStack trace:\n{ex.StackTrace}"; return; } // Update status text to show operation completion StatusText.Text = $"Data for item \"{portalItem.Title}\" (ID: {portalItem.ItemId}) retrieved successfully"; }