private async void btnSelectDownloadFile_Click(object sender, RoutedEventArgs e) { if (this.liveClient == null) { this.ShowMessage("Please sign in first."); return; } if (string.IsNullOrEmpty(this.tbdownloadUrl.Text)) { this.ShowMessage("Please specify the link to the file to be downloaded."); return; } try { string fileLink = this.tbdownloadUrl.Text; var roamingSettings = ApplicationData.Current.RoamingSettings; roamingSettings.Values["FileName"] = this.fileName; var appSettingContainer = roamingSettings.CreateContainer( "FileUploadDownload Settings", ApplicationDataCreateDisposition.Always); appSettingContainer.Values[this.fileName] = true; var roamingFolder = ApplicationData.Current.RoamingFolder; var storageDir = await roamingFolder.CreateFolderAsync( "FileUploadDownload sample", CreationCollisionOption.OpenIfExists); var storageFile = await storageDir.CreateFileAsync(this.fileName, CreationCollisionOption.ReplaceExisting); if (storageFile != null) { this.progressBar.Value = 0; var progressHandler = new Progress<LiveOperationProgress>( (progress) => { this.progressBar.Value = progress.ProgressPercentage; }); this.ShowProgress(); this.cts = new CancellationTokenSource(); LiveDownloadOperation operation = await this.liveClient.CreateBackgroundDownloadAsync( fileLink, storageFile); LiveDownloadOperationResult result = await operation.StartAsync(this.cts.Token, progressHandler); this.ShowMessage("Download completed."); } } catch (TaskCanceledException) { this.ShowMessage("User has cancelled the operation."); } catch (Exception exp) { this.ShowMessage(exp.ToString()); } }
private async void DownloadFile() { if (this.liveClient == null) { this.ShowMessage("Please sign in first."); return; } try { string filePath = this.tbUrl.Text; if (string.IsNullOrEmpty(filePath)) { this.ShowMessage("Please specify a file id or a url."); } else { this.progressBar.Value = 0; this.ShowProgress(); var progressHandler = new Progress <LiveOperationProgress>( (progress) => { this.progressBar.Value = progress.ProgressPercentage; }); this.cts = new CancellationTokenSource(); LiveDownloadOperation op = await this.liveClient.CreateBackgroundDownloadAsync(filePath); LiveDownloadOperationResult downloadResult = await op.StartAsync(this.cts.Token, progressHandler); if (downloadResult.Stream != null) { using (IRandomAccessStream ras = await downloadResult.GetRandomAccessStreamAsync()) { var imgSource = new BitmapImage(); imgSource.SetSource(ras); this.imgPreview.Source = imgSource; this.ShowImage(); } } else { this.ShowMessage("Download failed."); } } } catch (TaskCanceledException) { this.ShowMessage("User has cancelled the operation."); } catch (Exception exp) { this.ShowMessage(exp.ToString()); } }
private async void downButton_Click_1(object sender, RoutedEventArgs e) //下载 { try { msgText.Text = "亲:正在下载"; string id = string.Empty; var authClient = new LiveAuthClient(); var authResult = await authClient.LoginAsync(new string[] { "wl.skydrive" }); if (authResult.Session != null) { var connectClient = new LiveConnectClient(authResult.Session); LiveOperationResult operationResult = await connectClient.GetAsync("me/skydrive/search?q=WorkBook.xml"); List <object> items = operationResult.Result["data"] as List <object>; IDictionary <string, object> file = items.First() as IDictionary <string, object>; id = file["id"].ToString(); LiveDownloadOperation operation = await connectClient.CreateBackgroundDownloadAsync(string.Format("{0}/content", id)); var result = await operation.StartAsync(); Stream stream = result.Stream.AsStreamForRead(); StorageFolder localFolder = ApplicationData.Current.LocalFolder; StorageFile files = await localFolder.GetFileAsync(App.WordBookFileName); XDocument xBook = XDocument.Load(stream); await FileIO.WriteTextAsync(files, xBook.ToString()); msgText.Text = "恭喜:您已成功从OneDrive下载生词!"; } } catch (Exception ex) { msgText.Text = ex.Message; } }
private async void downButton_Click_1(object sender, RoutedEventArgs e)//下载 { try { msgText.Text = "亲:正在下载"; string id = string.Empty; var authClient = new LiveAuthClient(); var authResult = await authClient.LoginAsync(new string[] { "wl.skydrive" }); if (authResult.Session != null) { var connectClient = new LiveConnectClient(authResult.Session); LiveOperationResult operationResult = await connectClient.GetAsync("me/skydrive/search?q=notes.json"); List <object> items = operationResult.Result["data"] as List <object>; IDictionary <string, object> file = items.First() as IDictionary <string, object>; id = file["id"].ToString(); LiveDownloadOperation operation = await connectClient.CreateBackgroundDownloadAsync(string.Format("{0}/content", id)); var result = await operation.StartAsync(); Stream stream = result.Stream.AsStreamForRead(); Notes = (ObservableCollection <Note>)jsonSerializer.ReadObject(stream); StorageFile files = await ApplicationData.Current.LocalFolder.GetFileAsync(fileName); using (var streamJson = await ApplicationData.Current.LocalFolder.OpenStreamForWriteAsync(fileName, CreationCollisionOption.ReplaceExisting)) { jsonSerializer.WriteObject(streamJson, Notes); } msgText.Text = "恭喜:您已成功从OneDrive下载记事!"; Frame.Navigate(typeof(MainPage)); } } catch (Exception ex) { msgText.Text = ex.Message; } }
private async void GetProfilePicture() { try { LiveDownloadOperation operation = await this.liveClient.CreateBackgroundDownloadAsync("me/picture"); LiveDownloadOperationResult result = await operation.StartAsync(); if (result != null && result.Stream != null) { using (IRandomAccessStream ras = await result.GetRandomAccessStreamAsync()) { BitmapImage imgSource = new BitmapImage(); imgSource.SetSource(ras); this.imgMe.Source = imgSource; } } } catch (LiveConnectException e) { this.tbResponse.Text = e.ToString(); } }
private async Task LoadProfileImage(LiveConnectClient connectClient) { try { LiveDownloadOperation downloadOperation = await connectClient.CreateBackgroundDownloadAsync("me/picture"); LiveDownloadOperationResult result = await downloadOperation.StartAsync(); if (result != null && result.Stream != null) { using (IRandomAccessStream ras = await result.GetRandomAccessStreamAsync()) { BitmapImage imgSource = new BitmapImage(); imgSource.SetSource(ras); profileImage.Source = imgSource; } } } catch (LiveConnectException) { // Handle error cases. } }
private async void RunButton_Click(object sender, RoutedEventArgs e) { string message = null; try { // Ensure that the user has consented the wl.skydrive and wl.skydrive_update scopes var authClient = new LiveAuthClient(); var authResult = await authClient.LoginAsync(new string[] { "wl.skydrive", "wl.skydrive_update" }); if (authResult.Session == null) { throw new InvalidOperationException("You need to sign in and give consent to the app."); } var liveConnectClient = new LiveConnectClient(authResult.Session); // Validate parameters var imageSourceUrl = imgSourceUrlTextBox.Text; if (string.IsNullOrWhiteSpace(imageSourceUrl)) { throw new InvalidOperationException("Image Url is empty."); } var uploadPath = uploadPathTextBox.Text; if (string.IsNullOrWhiteSpace(uploadPath)) { throw new InvalidOperationException("Upload location is empty."); } // Download the image from the Internet var networkFileDownloader = new BackgroundDownloader(); DownloadOperation networkFileDownloadOperation = networkFileDownloader.CreateDownload(new Uri(imageSourceUrl), null); await networkFileDownloadOperation.StartAsync(); IInputStream uploadInputStream = networkFileDownloadOperation.GetResultStreamAt(0); // Upload to OneDrive LiveUploadOperation uploadOperation = await liveConnectClient.CreateBackgroundUploadAsync(uploadPath, "ImageFromInternet.jpg", uploadInputStream, OverwriteOption.Rename); LiveOperationResult uploadResult = await uploadOperation.StartAsync(); dynamic uploadedResource = uploadResult.Result; string uploadedResourceId = uploadedResource.id; string uploadedResourceName = uploadedResource.name; string uploadedResourceLink = uploadedResource.source; uploadedResourceTextBox.Text = string.Format("{0}\r\n{1}\r\n{2}", uploadedResourceId, uploadedResourceName, uploadedResourceLink); // Download from the OneDrive LiveDownloadOperation downloadOperation = await liveConnectClient.CreateBackgroundDownloadAsync(uploadedResourceId + "/content"); LiveDownloadOperationResult downloadResult = await downloadOperation.StartAsync(); if (downloadResult != null && downloadResult.Stream != null) { using (IRandomAccessStream ras = await downloadResult.GetRandomAccessStreamAsync()) { BitmapImage imgSource = new BitmapImage(); imgSource.SetSource(ras); skydriveImage.Source = imgSource; } } } catch (Exception ex) { message = "Operation failed: " + ex.Message; } if (message != null) { var dialog = new Windows.UI.Popups.MessageDialog(message); await dialog.ShowAsync(); } }