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); }
BackgroundTransferContentPart GetTextPart(string key, string value) { // namesh mishe KEY va textesh mishe VALUE BackgroundTransferContentPart part = new BackgroundTransferContentPart(key); // chon typeshon text hast va utf-8 bayad content typeshon ro set konim be in part.SetHeader("Content-Type", "text/plain; charset=utf-8"); part.SetText(value); // sare ham ye chize in shekli mogheye upload ijad mishe: //Content-Type: text/plain; charset=utf-8 //Content-Disposition: form-data; name="_uuid" // //86665e74-5514-8236-basu-15aef8ceb0d6 return(part); }
/// <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)); }
public async Task <ResponseInfo> UploadAsync(string remoteUri, string file, bool toastNotify = true) { uploader = new BackgroundUploader(); uploader.Method = "POST"; if (notifyTemplate != null && toastNotify) { uploader.SuccessToastNotification = new Windows.UI.Notifications.ToastNotification(notifyTemplate); } StorageFile storageFile = await StorageFile.GetFileFromPathAsync(file); BackgroundTransferContentPart part = new BackgroundTransferContentPart("file", storageFile.Name); part.SetFile(storageFile); List <BackgroundTransferContentPart> parts = new List <BackgroundTransferContentPart>(); parts.Add(part); UploadOperation operation = await uploader.CreateUploadAsync(new Uri(remoteUri), parts); return(await GetUploadResponse(operation)); }
// 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) { } }
List <BackgroundTransferContentPart> PrepareRequest(BackgroundUploader uploader, string tag, IDictionary <string, string> headers = null, IDictionary <string, string> parameters = null) { List <BackgroundTransferContentPart> parts = new List <BackgroundTransferContentPart>(); if (headers != null) { foreach (string key in headers.Keys) { if (!string.IsNullOrEmpty(headers[key])) { uploader.SetRequestHeader(key, headers[key]); } } } if (parameters != null) { foreach (string key in parameters.Keys) { if (parameters[key] != null) { BackgroundTransferContentPart part = new BackgroundTransferContentPart(key); part.SetText(parameters[key]); parts.Add(part); } } } if (tag.Length > 40) { tag = tag.Substring(0, 40); } uploader.TransferGroup = BackgroundTransferGroup.CreateGroup(tag); return(parts); }
// 新增一个上传任务(一次请求上传多个文件) 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); }
/// <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); }