Created
January 4, 2013 07:10
-
-
Save anonymous/4450584 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private void fetchFileButton_Click(object sender, RoutedEventArgs e) | |
| { | |
| //// (1) 檢查transfer request是否超過數量 | |
| if (BackgroundTransferService.Requests.Count() >= 5) | |
| { | |
| MessageBox.Show("此應用程序的背景文件傳輸請求的最大數量已超過。"); | |
| return; | |
| } | |
| //// (2) 創建新的transfer request | |
| string transferFileName = filenameTextBlock.Text; | |
| //將 URI 字串轉換成它的逸出表示。 | |
| Uri transferUri = new Uri(Uri.EscapeUriString(transferFileName), UriKind.RelativeOrAbsolute); | |
| //創建新的transfer request | |
| transferRequest = new BackgroundTransferRequest(transferUri); | |
| // GET and POST 皆有,但是不支援FTP協定 | |
| transferRequest.Method = "GET"; | |
| //// (3) 設定request 1.下載目標位置 2.標籤 3.何種條件transfer可被使用 | |
| //從URI拿取檔案的名字,並且創建一個transfers目錄下的local URI。(放在隔離存取區內) | |
| string downloadFile = transferFileName.Substring(transferFileName.LastIndexOf("/") + 1); | |
| //因為下載的檔案必須指定儲存的實體位置,上傳也需要指定實體位置 | |
| //統一所有background tansfers使用的檔案路徑,必須存在於isolated storage的固定路徑:「/shared/transfers」之中 | |
| //建立URI | |
| downloadUri = new Uri("shared/transfers/" + downloadFile, UriKind.RelativeOrAbsolute); | |
| transferRequest.DownloadLocation = downloadUri;//設定下載目標儲存位置 | |
| //把抓取到的名字"picture.jpg"放入tag | |
| transferRequest.Tag = downloadFile; | |
| //設定允許傳遞的狀態 | |
| transferRequest.TransferPreferences = TransferPreferences.AllowCellularAndBattery; | |
| //// (4) 啟動request | |
| // 加入transfer request至BackgroundTransferService | |
| try | |
| { | |
| BackgroundTransferService.Add(transferRequest); | |
| } | |
| catch (InvalidOperationException ex) | |
| { | |
| MessageBox.Show("Unable to add background transfer request. " + ex.Message); | |
| } | |
| catch (Exception) | |
| { | |
| MessageBox.Show("Unable to add background transfer request."); | |
| } | |
| // Navigate back | |
| NavigationService.GoBack(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment