private async void btnSelectUploadFile_Click(object sender, RoutedEventArgs e) { if (this.liveClient == null) { this.ShowMessage("Please sign in first."); return; } if (string.IsNullOrEmpty(this.tbuploadUrl.Text)) { this.ShowMessage("Please specify the upload folder path."); return; } try { string folderPath = this.tbuploadUrl.Text; var picker = new FileOpenPicker { ViewMode = PickerViewMode.Thumbnail, SuggestedStartLocation = PickerLocationId.PicturesLibrary }; picker.FileTypeFilter.Add("*"); StorageFile file = await picker.PickSingleFileAsync(); if (file != null) { this.fileName = file.Name; this.progressBar.Value = 0; var progressHandler = new Progress<LiveOperationProgress>( (progress) => { this.progressBar.Value = progress.ProgressPercentage; }); this.ShowProgress(); this.cts = new CancellationTokenSource(); LiveUploadOperation operation = await this.liveClient.CreateBackgroundUploadAsync( folderPath, file.Name, file, OverwriteOption.Rename); LiveOperationResult result = await operation.StartAsync( this.cts.Token, progressHandler); dynamic fileData = result.Result; string downloadUrl = fileData.id + "/content"; this.tbdownloadUrl.Text = downloadUrl; this.ShowMessage("Upload completed"); } } catch (TaskCanceledException) { this.ShowMessage("User has cancelled the operation."); } catch (Exception exp) { this.ShowMessage(exp.ToString()); } }
private async void uploadButton_Click(object sender, RoutedEventArgs e)//上传 { try { msgText.Text = "亲:正在上传"; var authClient = new LiveAuthClient(); var authResult = await authClient.LoginAsync(new string[] { "wl.skydrive", "wl.skydrive_update" }); if (authResult.Session != null) { var liveConnectClient = new LiveConnectClient(authResult.Session); StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync(fileName); //String fileText = await FileIO.ReadTextAsync(file); LiveUploadOperation uploadOperation = await liveConnectClient.CreateBackgroundUploadAsync( "me/skydrive", file.Name, file, OverwriteOption.Overwrite); LiveOperationResult uploadResult = await uploadOperation.StartAsync(); if (uploadResult.Result != null) { msgText.Text = "恭喜:您已成功同步记事至OneDrive!"; } } } catch (LiveAuthException ex) { msgText.Text = ex.Message; } catch (LiveConnectException ex) { msgText.Text = ex.Message; } }
private async void UploadFile() { if (this.liveClient == null) { this.ShowMessage("Please sign in first."); return; } try { string folderPath = this.tbUrl.Text; var picker = new FileOpenPicker { ViewMode = PickerViewMode.Thumbnail, SuggestedStartLocation = PickerLocationId.PicturesLibrary }; picker.FileTypeFilter.Add("*"); StorageFile file = await picker.PickSingleFileAsync(); if (file != null) { using (IInputStream inStream = await file.OpenReadAsync().AsTask()) { this.progressBar.Value = 0; this.ShowProgress(); var progressHandler = new Progress <LiveOperationProgress>( (progress) => { this.progressBar.Value = progress.ProgressPercentage; }); this.cts = new CancellationTokenSource(); LiveUploadOperation operation = await this.liveClient.CreateBackgroundUploadAsync( folderPath, file.Name, inStream, OverwriteOption.Rename); LiveOperationResult result = await operation.StartAsync(this.cts.Token, progressHandler); if (result != null) { this.progressBar.Value = 0; dynamic fileData = result.Result; string downloadUrl = fileData.id + "/picture"; this.tbUrl.Text = downloadUrl; this.ShowMessage("Upload completed."); } else { this.ShowMessage("Upload failed."); } } } } catch (TaskCanceledException) { this.ShowMessage("User has cancelled the operation."); } catch (Exception exp) { this.ShowMessage(exp.ToString()); } }
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(); } }