Windows8 BackgroundTransfer UpLoad

        private CancellationTokenSource cts;

        public ScenarioInput2()
        {
            InitializeComponent();

            cts = new CancellationTokenSource();
        }

        async void FrameLoaded(object sender, object e)
        {
            await DiscoverActiveUploadsAsync();
        }


        private async Task DiscoverActiveUploadsAsync()
        {
            IReadOnlyList<UploadOperation> uploads = null;
            try
            {
                uploads = await BackgroundUploader.GetCurrentUploadsAsync();

                if (uploads.Count > 0)
                {
                    List<Task> tasks = new List<Task>();
                    foreach (UploadOperation upload in uploads)
                    {
                        tasks.Add(HandleUploadAsync(upload, false));
                    }
                    await Task.WhenAll(tasks);
                }
            }
            catch (Exception ex){ return; }
        }


        private async Task HandleUploadAsync(UploadOperation upload, bool isNewUpload)
        {
            try
            {
                Progress<UploadOperation> progressCallback = new Progress<UploadOperation>(UploadProgress);
                if (isNewUpload)
                {
                    // Start the upload and attach a progress handler.
                    await upload.StartAsync().AsTask(cts.Token, progressCallback);
                }
                else
                {
                    // The upload was already running when the application started, re-attach the progress handler.
                    await upload.AttachAsync().AsTask(cts.Token, progressCallback);
                }
                ResponseInformation response = upload.GetResponseInformation();
            }
            catch (TaskCanceledException) { }
            catch (Exception ex) { }
        }


        private void UploadProgress(UploadOperation upload)
        {
            BackgroundUploadProgress progress = upload.Progress;

            double percentSent = 100;
            if (progress.TotalBytesToSend > 0)
                percentSent = progress.BytesSent * 100 / progress.TotalBytesToSend;

            if (progress.HasRestarted)
                Debug.WriteLine(" - Upload Restarted");
        }


        private async void StartUpload_Click(object sender, RoutedEventArgs e)
        {
            Uri uri;
            if (!Uri.TryCreate(this.serverAddressField.Text.Trim(), UriKind.Absolute, out uri))
                return;

            FileOpenPicker picker = new FileOpenPicker();
            picker.FileTypeFilter.Add("*");
            StorageFile file = await picker.PickSingleFileAsync();
            if (file == null)
                return;

            BackgroundUploader uploader = new BackgroundUploader();
            uploader.SetRequestHeader("msiFileName", file.Name);//用于设置HTTP请求标头

            UploadOperation upload = uploader.CreateUpload(uri, file);
            await HandleUploadAsync(upload, true);
        }


        private async void StartMultipartUpload_Click(object sender, RoutedEventArgs e)
        {
            Uri uri;
            if (!Uri.TryCreate(this.serverAddressField.Text.Trim(), UriKind.Absolute, out uri))
                return;

            FileOpenPicker picker = new FileOpenPicker();
            picker.FileTypeFilter.Add("*");
            IReadOnlyList<StorageFile> files = await picker.PickMultipleFilesAsync();



            List<BackgroundTransferContentPart> parts = new List<BackgroundTransferContentPart>();
            for (int i = 0; i < files.Count; i++)
            {
                BackgroundTransferContentPart part = new BackgroundTransferContentPart("msiFile" + i, files[i].Name);
                part.SetFile(files[i]);

                parts.Add(part);
            }

            BackgroundUploader uploader = new BackgroundUploader();

            UploadOperation upload = await uploader.CreateUploadAsync(uri, parts);
            await HandleUploadAsync(upload, true);
        }

        private void CancelAll_Click(object sender, RoutedEventArgs e)
        {
            cts.Cancel();
            cts.Dispose();

            // Re-create the CancellationTokenSource and activeUploads for future uploads.
            cts = new CancellationTokenSource();
        }

 

posted on 2013-03-28 15:08  JackSlaterYu  阅读(280)  评论(0编辑  收藏  举报