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)); }
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> 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); }
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 BackgroundTransferContentPart CreateContentPart(IUpload upload, string fieldName) { var part = new BackgroundTransferContentPart(fieldName, $"{upload.Uploadable.Name}{upload.Uploadable.Extension}"); part.SetHeader("Content-Type", upload.Uploadable.Source.GetContentType()); part.SetFile(upload.Uploadable.Source.GetFile()); return(part); }
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); }
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); }
/// <summary> /// Create the BackgroundTransferContentPart list. /// </summary> /// <param name="files">The file list.</param> /// <returns> /// The BackgroundTransferContentPart list created. /// </returns> private List <BackgroundTransferContentPart> CreateBackgroundTransferContentPartList(IReadOnlyList <StorageFile> files) { if (files == null) { return(null); } 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); } return(parts); }
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)); }
// private const string bucketurl = "http://s3.amazonaws.com/"; // private const string bucketurl = "com.mf.carl-prototype.s3.amazonaws.com"; #region Upload public async Task UploadFile(IReadOnlyList<StorageFile> files) { try { basicAwsCredentials = new BasicAWSCredentials(accesskey, secretkey); List<BackgroundTransferContentPart> parts = new List<BackgroundTransferContentPart>(); for (int i = 0; i < files.Count; i++) { BackgroundTransferContentPart part = new BackgroundTransferContentPart(files[i].Name, files[i].Name); this.Filename = files[i].Name; part.SetFile(files[i]); parts.Add(part); } //Uri uri = new Uri(bucketurl + ExistingBucketName + "/"); //Uri uri = new Uri("https://com.mf.carl-prototype.s3-us-west-2.amazonaws.com/"); Uri uri = new Uri("https://s3.amazonaws.com/" + ExistingBucketName +"/"); // Uri uri = new Uri("https://"+ExistingBucketName+".s3-us-west-2.amazonaws.com/" ); BackgroundUploader uploader = new BackgroundUploader(); PasswordCredential pwdCredential = new PasswordCredential(); pwdCredential.UserName = accesskey; pwdCredential.Password = secretkey; uploader.ServerCredential = pwdCredential; // uploader.Method = "POST"; // uploader.SetRequestHeader("Content-Type", "multipart/form-data;boundary=34"); uploader.ServerCredential = new PasswordCredential{ UserName=accesskey, Password= secretkey}; UploadOperation upload = await uploader.CreateUploadAsync(uri, parts); // Attach progress and completion handlers. await HandleUploadAsync(upload, true); } catch(Exception ex) { } }
// 新增一个上传任务(一次请求上传多个文件) 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); }
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 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); }
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); }
/// <summary> /// Create the BackgroundTransferContentPart list. /// </summary> /// <param name="files">The file list.</param> /// <returns> /// The BackgroundTransferContentPart list created. /// </returns> private List<BackgroundTransferContentPart> CreateBackgroundTransferContentPartList(IReadOnlyList<StorageFile> files) { if (files == null) return null; 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); } return parts; }
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); }