private static async Task <UploadOperation> CreateUploadOperationForCreateVideo( IStorageFile file, string token, BackgroundUploader uploader) { const string boundary = "videoboundary"; List <BackgroundTransferContentPart> parts = new List <BackgroundTransferContentPart>(); BackgroundTransferContentPart metadataPart = new BackgroundTransferContentPart("token"); metadataPart.SetText(token); //metadataPart.SetText("iamatoken"); parts.Add(metadataPart); BackgroundTransferContentPart videoPart = new BackgroundTransferContentPart("content_file", file.Name); videoPart.SetFile(file); videoPart.SetHeader("Content-Type", file.ContentType); parts.Add(videoPart); return (await uploader.CreateUploadAsync( new Uri(AddVideoPostUri), parts, "form-data", boundary)); }
private static async Task <UploadOperation> CreateUploadOperationForCreateImage( IStorageFile file, string token, BackgroundUploader uploader) { const string boundary = "imgboundary"; List <BackgroundTransferContentPart> parts = new List <BackgroundTransferContentPart>(); BackgroundTransferContentPart metadataPart = new BackgroundTransferContentPart("token"); metadataPart.SetText(token); //metadataPart.SetText("iamatoken"); parts.Add(metadataPart); BackgroundTransferContentPart imagePart = new BackgroundTransferContentPart("photo", file.Name); imagePart.SetFile(file); imagePart.SetHeader("Content-Type", file.ContentType); parts.Add(imagePart); return (await uploader.CreateUploadAsync( new Uri(HttpFotosSapoPtUploadpostHtmlUri), parts, "form-data", boundary)); }
public async Task <ResponseInfo> UploadAsync(string remoteUri, string[] files, bool toastNotify = true) { uploader = new BackgroundUploader(); uploader.Method = "POST"; if (notifyTemplate != null && toastNotify) { uploader.SuccessToastNotification = new Windows.UI.Notifications.ToastNotification(notifyTemplate); } List <BackgroundTransferContentPart> parts = new List <BackgroundTransferContentPart>(); foreach (var f in files) { StorageFile storageFile = await StorageFile.GetFileFromPathAsync(f); if (storageFile != null) { BackgroundTransferContentPart part = new BackgroundTransferContentPart("file", storageFile.Name); part.SetFile(storageFile); parts.Add(part); } } UploadOperation uploadOpration = await uploader.CreateUploadAsync(new Uri(remoteUri), parts, "form-data"); return(await GetUploadResponse(uploadOpration)); }
private async void StartMultipartUpload_Click(object sender, RoutedEventArgs e) { // By default 'serverAddressField' is disabled and URI validation is not required. When enabling the text // box validating the URI is required since it was received from an untrusted source (user input). // The URI is validated by calling Uri.TryCreate() that will return 'false' for strings that are not valid URIs. // Note that when enabling the text box users may provide URIs to machines on the intrAnet that require // the "Home or Work Networking" capability. Uri uri; if (!Uri.TryCreate(serverAddressField.Text.Trim(), UriKind.Absolute, out uri)) { rootPage.NotifyUser("Invalid URI.", NotifyType.ErrorMessage); return; } // Verify that we are currently not snapped, or that we can unsnap to open the picker. if (ApplicationView.Value == ApplicationViewState.Snapped && !ApplicationView.TryUnsnap()) { rootPage.NotifyUser("File picker cannot be opened in snapped mode. Please unsnap first.", NotifyType.ErrorMessage); return; } FileOpenPicker picker = new FileOpenPicker(); picker.FileTypeFilter.Add("*"); IReadOnlyList <StorageFile> files = await picker.PickMultipleFilesAsync(); if (files.Count == 0) { rootPage.NotifyUser("No file selected.", NotifyType.ErrorMessage); return; } List <BackgroundTransferContentPart> parts = new List <BackgroundTransferContentPart>(); for (int i = 0; i < files.Count; i++) { BackgroundTransferContentPart part = new BackgroundTransferContentPart("File" + i, files[i].Name); part.SetFile(files[i]); parts.Add(part); } BackgroundUploader uploader = new BackgroundUploader(); UploadOperation upload = await uploader.CreateUploadAsync(uri, parts); String fileNames = files[0].Name; for (int i = 1; i < files.Count; i++) { fileNames += ", " + files[i].Name; } Log(String.Format("Uploading {0} to {1}, {2}", fileNames, uri.AbsoluteUri, upload.Guid)); // Attach progress and completion handlers. await HandleUploadAsync(upload, true); }
public async Task <FileUploadResponse> UploadFileAsync(string url, FilePathItem[] fileItems, string tag, IDictionary <string, string> headers = null, IDictionary <string, string> parameters = null, string boundary = null) { if (fileItems == null || fileItems.Length == 0) { var fileUploadResponse = new FileUploadResponse("There are no items to upload", -1, tag, null); FileUploadError(this, fileUploadResponse); return(fileUploadResponse); } Uri uri; if (!Uri.TryCreate(url.Trim(), UriKind.Absolute, out uri)) { var fileUploadResponse = new FileUploadResponse("Invalid upload url", -1, tag, null); FileUploadError(this, fileUploadResponse); return(fileUploadResponse); } BackgroundUploader uploader = new BackgroundUploader(); var parts = PrepareRequest(uploader, tag, headers, parameters); for (int i = 0; i < fileItems.Length; i++) { BackgroundTransferContentPart part = new BackgroundTransferContentPart(fileItems[i].FieldName, fileItems[i].Path.Substring(fileItems[i].Path.LastIndexOf("/") + 1)); var storageFile = await StorageFile.GetFileFromPathAsync(@fileItems[i].Path); part.SetFile(storageFile); parts.Add(part); } UploadOperation upload = null; if (string.IsNullOrEmpty(boundary)) { upload = await uploader.CreateUploadAsync(uri, parts); } else { upload = await uploader.CreateUploadAsync(uri, parts, "form-data", boundary); } return(await HandleUploadAsync(upload, true)); }
/// <summary> /// Invoked when image add button is clicked and choose the image. /// </summary> /// <param name="sender">The image add button clicked.</param> /// <param name="e">Event data that describes how the click was initiated.</param> private async void ImageUploadButton_Click(object sender, RoutedEventArgs e) { FileOpenPicker picker = FileTypePicker(imagesFilterTypeList); if (picker == null) { return; } images = await picker.PickMultipleFilesAsync(); if (images == null || images.Count == 0) { return; } Image imgImg = new Image { Source = new BitmapImage(new Uri("ms-appx:///Images/Upload/image.png")), Margin = new Thickness(5, 0, 5, 0) }; ToolTip toolTip = new ToolTip(); toolTip.Content = images[0].Name; ToolTipService.SetToolTip(imgImg, toolTip); totalImagePanel.Children.RemoveAt(totalImagePanel.Children.Count - 1); imagePanel.Children.Add(imgImg); List <BackgroundTransferContentPart> imageParts = CreateBackgroundTransferContentPartList(images); Uri uploadUri = new Uri(Constants.DataCenterURI + "Upload.aspx?username="******"Image uplaod error3. Please check your network."); } }
private async void UploadMultipleFiles(Uri uri, IReadOnlyList <StorageFile> files) { if (files.Count == 0) { rootPage.NotifyUser("No file selected.", NotifyType.ErrorMessage); return; } ulong totalFileSize = 0; for (int i = 0; i < files.Count; i++) { BasicProperties properties = await files[i].GetBasicPropertiesAsync(); totalFileSize += properties.Size; if (totalFileSize > maxUploadFileSize) { rootPage.NotifyUser(String.Format(CultureInfo.CurrentCulture, "Size of selected files exceeds max. upload file size ({0} MB).", maxUploadFileSize / (1024 * 1024)), NotifyType.ErrorMessage); return; } } List <BackgroundTransferContentPart> parts = new List <BackgroundTransferContentPart>(); for (int i = 0; i < files.Count; i++) { BackgroundTransferContentPart part = new BackgroundTransferContentPart("File" + i, files[i].Name); part.SetFile(files[i]); parts.Add(part); } BackgroundUploader uploader = new BackgroundUploader(); UploadOperation upload = await uploader.CreateUploadAsync(uri, parts); String fileNames = files[0].Name; for (int i = 1; i < files.Count; i++) { fileNames += ", " + files[i].Name; } Log(String.Format(CultureInfo.CurrentCulture, "Uploading {0} to {1}, {2}", fileNames, uri.AbsoluteUri, upload.Guid)); // Attach progress and completion handlers. await HandleUploadAsync(upload, true); }
private const int maxUploadFileSize = 100 * 1024 * 1024; // 100 MB /// <summary> /// StartUploadFiles /// </summary> /// <param name="url"></param> /// <param name="files"></param> public async void StartUploadFiles(string url, IReadOnlyList <StorageFile> files) { Uri uri = null; if (!Uri.TryCreate(url, UriKind.Absolute, out uri)) { if (this.ErrorException != null) { this.ErrorException(this, "Please Invalid UploadURl"); } return; } if (files == null || files.Count == 0) { if (this.ErrorException != null) { this.ErrorException(this, "Please Invalid files"); } return; } ulong totalFileSize = 0; for (int i = 0; i < files.Count; i++) { BasicProperties properties = await files[i].GetBasicPropertiesAsync(); totalFileSize += properties.Size; if (totalFileSize > maxUploadFileSize) { if (this.ErrorException != null) { this.ErrorException(this, "Please Invalid maxUploadFileSize"); } return; } } List <BackgroundTransferContentPart> parts = new List <BackgroundTransferContentPart>(); for (int i = 0; i < files.Count; i++) { BackgroundTransferContentPart part = new BackgroundTransferContentPart("File" + 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 async Task <Tuple <UploadOperation, ICompletedUpload> > CreateUploadOperationAsync( IEnumerable <BackgroundTransferContentPart> parts, IUpload upload) { var uploader = new BackgroundUploader(); uploader.TransferGroup = _transferGroup; AttachNotifications(uploader, upload); var operation = await uploader.CreateUploadAsync(new Uri(upload.UploadUrl), parts); var completedUpload = new CompletedUpload { Id = operation.Guid, Name = upload.Uploadable.Name, ContentType = upload.Uploadable.ContentType }; await _database.InsertAsync(completedUpload); return(new Tuple <UploadOperation, ICompletedUpload>(operation, completedUpload)); }
// 新增一个上传任务(一次请求上传多个文件) private async void btnAddMultiUpload_Click(object sender, RoutedEventArgs e) { // 上传服务的地址 Uri serverUri = new Uri("http://localhost:44914/api/Upload", UriKind.Absolute); // 需要上传的文件源集合 List <StorageFile> sourceFiles = new List <StorageFile>(); for (int i = 0; i < 3; i++) { StorageFile sourceFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/hololens.jpg", UriKind.Absolute)); sourceFiles.Add(sourceFile); } // 构造需要上传 BackgroundTransferContentPart 集合 List <BackgroundTransferContentPart> contentParts = new List <BackgroundTransferContentPart>(); for (int i = 0; i < sourceFiles.Count; i++) { BackgroundTransferContentPart contentPart = new BackgroundTransferContentPart("File" + i, sourceFiles[i].Name); contentPart.SetFile(sourceFiles[i]); contentParts.Add(contentPart); } BackgroundUploader backgroundUploader = new BackgroundUploader(); // 任务成功后弹出指定的 toast 通知(类似的还有 SuccessTileNotification, FailureToastNotification, FailureTileNotification) backgroundUploader.SuccessToastNotification = GetToastNotification(); // 创建一个后台上传任务,此任务包含多个上传文件 UploadOperation upload = await backgroundUploader.CreateUploadAsync(serverUri, contentParts); // 处理并监视指定的后台上传任务 await HandleUploadAsync(upload, true); }
/// <summary> /// Invoked when upload button in popup is clicked and add new lesson. /// </summary> /// <param name="sender">The upload button clicked.</param> /// <param name="e">Event data that describes how the click was initiated.</param> private async void UploadLessionButton_Click(object sender, RoutedEventArgs e) { if (!CheckLessonInfomation()) { ShowMessageDialog("Format error1! Please check your upload infomation."); return; } lessonUploadProgressRing.IsActive = true; UploadLessionButton.Visibility = Visibility.Collapsed; CancelUploadButton.Visibility = Visibility.Collapsed; allLessons.Add(new Lesson(++lessonCount, lessonName.Text, lessonDescription.Text)); List <BackgroundTransferContentPart> docParts = CreateBackgroundTransferContentPartList(docs); List <BackgroundTransferContentPart> audioParts = CreateBackgroundTransferContentPartList(audios); List <BackgroundTransferContentPart> videoParts = CreateBackgroundTransferContentPartList(videos); List <BackgroundTransferContentPart> allParts = new List <BackgroundTransferContentPart>(); if (docParts != null) { allParts.AddRange(docParts); } if (audioParts != null) { allParts.AddRange(audioParts); } if (videoParts != null) { allParts.AddRange(videoParts); } Uri uploadUri = new Uri(Constants.DataCenterURI + "Upload.aspx?username="******"Network connection error2!"); lessonCount--; ResetPopup(); return; } AddLessonInfo(); lessonScrollView.ScrollToVerticalOffset(lessonScrollView.VerticalOffset); lessonUploadProgressRing.IsActive = false; UploadLessionButton.Visibility = Visibility.Visible; CancelUploadButton.Visibility = Visibility.Visible; wholeFrame.Opacity = 1; addLessonPopup.IsOpen = false; }
private async void StartMultipartUpload_Click(object sender, RoutedEventArgs e) { // By default 'serverAddressField' is disabled and URI validation is not required. When enabling the text // box validating the URI is required since it was received from an untrusted source (user input). // The URI is validated by calling Uri.TryCreate() that will return 'false' for strings that are not valid URIs. // Note that when enabling the text box users may provide URIs to machines on the intrAnet that require // the "Home or Work Networking" capability. Uri uri; if (!Uri.TryCreate(serverAddressField.Text.Trim(), UriKind.Absolute, out uri)) { rootPage.NotifyUser("Invalid URI.", NotifyType.ErrorMessage); return; } FileOpenPicker picker = new FileOpenPicker(); picker.FileTypeFilter.Add("*"); IReadOnlyList <StorageFile> files = await picker.PickMultipleFilesAsync(); if (files.Count == 0) { rootPage.NotifyUser("No file selected.", NotifyType.ErrorMessage); return; } ulong totalFileSize = 0; for (int i = 0; i < files.Count; i++) { BasicProperties properties = await files[i].GetBasicPropertiesAsync(); totalFileSize += properties.Size; if (totalFileSize > maxUploadFileSize) { rootPage.NotifyUser(String.Format(CultureInfo.CurrentCulture, "Size of selected files exceeds max. upload file size ({0} MB).", maxUploadFileSize / (1024 * 1024)), NotifyType.ErrorMessage); return; } } List <BackgroundTransferContentPart> parts = new List <BackgroundTransferContentPart>(); for (int i = 0; i < files.Count; i++) { BackgroundTransferContentPart part = new BackgroundTransferContentPart("File" + i, files[i].Name); part.SetFile(files[i]); parts.Add(part); } BackgroundUploader uploader = new BackgroundUploader(); UploadOperation upload = await uploader.CreateUploadAsync(uri, parts); String fileNames = files[0].Name; for (int i = 1; i < files.Count; i++) { fileNames += ", " + files[i].Name; } Log(String.Format(CultureInfo.CurrentCulture, "Uploading {0} to {1}, {2}", fileNames, uri.AbsoluteUri, upload.Guid)); // Attach progress and completion handlers. await HandleUploadAsync(upload, true); }