/// <summary> /// 上传图片至OneDrive /// </summary> /// <param name="image">图片文件</param> /// <returns></returns> public async Task <OneDriveImage> UploadImage(StorageFile image, OneDriveImageGroup group) { if (_appFolder == null) { throw new UnauthorizedAccessException("You need to complete OneDrive authorization before you can upload the image"); } var imageFolder = await GetImgSaveFolder(); var per = await image.GetBasicPropertiesAsync(); string fileId = ""; string name = ""; string extension = Path.GetExtension(image.Path); bool isAutoRename = Convert.ToBoolean(AppTools.GetLocalSetting(AppSettings.AutoRename, "True")); string fileName = isAutoRename ? Guid.NewGuid() + extension : image.Name.Trim(); OneDriveStorageFile cre = null; try { // 这里要对文件大小进行判断,以4MB为分水岭,需要用不同的办法上传 if (per.Size < 4 * 1024 * 1024) { using (var stream = await image.OpenReadAsync()) { cre = await imageFolder.StorageFolderPlatformService.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting, stream); fileId = cre.OneDriveItem.Id; name = cre.OneDriveItem.Name; } } else { using (var stream = await image.OpenReadAsync()) { cre = await imageFolder.StorageFolderPlatformService.UploadFileAsync(fileName, stream, CreationCollisionOption.ReplaceExisting, 320 * 1024); fileId = cre.OneDriveItem.Id; name = cre.OneDriveItem.Name; } } string link = LinkConvert(fileId); var item = new OneDriveImage(name, await AppTools.ConvertFileToImage(image), group, link, fileId); return(item); } catch (Exception) { return(null); } }
public async void FileUpload(List <StorageFile> imgList) { if (imgList.Count > 0) { // 弹出组选择框 var groupDialog = new GroupDialog(); groupDialog.PrimaryButtonClick += async(_s, _e) => { // 选定分组后,上传图片 if (groupDialog.SelectGroup != null) { var errorFile = new List <StorageFile>(); UploadInfoCollection.Clear(); UploadProgressCollection.Clear(); var imgItemList = new List <OneDriveImage>(); var imgTemp = await AppTools.ConvertFileToImage(imgList[0]); // 如果仅一张图片上传,则显示该图片的分辨率、大小等信息 if (imgList.Count == 1) { var fileInfo = await imgList[0].GetBasicPropertiesAsync(); UploadInfoCollection.Add(new KeyValue(AppTools.GetReswLanguage("RP"), imgTemp.PixelWidth + "x" + imgTemp.PixelHeight)); UploadInfoCollection.Add(new KeyValue(AppTools.GetReswLanguage("ImageSize"), Math.Round((fileInfo.Size / (1024.0 * 1024.0)), 1) + "M")); UploadInfoCollection.Add(new KeyValue(AppTools.GetReswLanguage("ImageType"), Path.GetExtension(imgList[0].Path).Substring(1).ToUpper())); UploadInfoCollection.Add(new KeyValue(AppTools.GetReswLanguage("CreateDate"), fileInfo.DateModified.ToString("yyyy/MM/dd"))); } // 如果有多张图片上传,则显示总览信息 else { double size = 0; foreach (var item in imgList) { var fileInfo = await item.GetBasicPropertiesAsync(); size += Math.Round((fileInfo.Size / (1024.0 * 1024.0)), 1); } UploadInfoCollection.Add(new KeyValue(AppTools.GetReswLanguage("ImageCount"), imgList.Count.ToString())); UploadInfoCollection.Add(new KeyValue(AppTools.GetReswLanguage("GroupName"), groupDialog.SelectGroup.GroupName)); UploadInfoCollection.Add(new KeyValue(AppTools.GetReswLanguage("AllSize"), size + "M")); } // 将上传列表的第一张图片作为上传区域背景 var backBrush = new ImageBrush(); backBrush.Stretch = Stretch.UniformToFill; backBrush.ImageSource = imgTemp; UploadArea.Background = backBrush; // 隐藏上传提示字符 UploadAreaHold.Visibility = Visibility.Collapsed; double uploadColumnWidth = UploadColumn.ActualWidth - 30; // 根据上传信息显示的数目来确定单一信息所占区域大小 UploadInfoWidth = uploadColumnWidth / UploadInfoCollection.Count; // 装载进度条 for (int i = 0; i < imgList.Count; i++) { var item = imgList[i]; UploadProgressCollection.Add(new ProgressStatus(i + 1, item.DisplayName)); } // 开始逐一上传图片 foreach (var item in imgList) { var img = await App.OneDriveTools.UploadImage(item, groupDialog.SelectGroup); // 图片若上传错误,则加入错误文件列表中 if (img == null) { errorFile.Add(item); } // 否则,写入成功列表 else { imgItemList.Add(img); LastestImageCollection.Insert(0, img); if (LastestNoDataTipBlock.Visibility == Visibility.Visible) { LastestNoDataTipBlock.Visibility = Visibility.Collapsed; LastestListView.Visibility = Visibility.Visible; } for (int j = UploadProgressCollection.Count - 1; j >= 0; j--) { if (UploadProgressCollection[j].Name.Replace($"[{j + 1}] ", "") == item.DisplayName) { UploadProgressCollection.RemoveAt(j); } } } } // 所有任务上传完成,清空背景,加入占位符 var res = App.Current.RequestedTheme == ApplicationTheme.Dark ? (ResourceDictionary)App.Current.Resources.ThemeDictionaries["Dark"] : (ResourceDictionary)App.Current.Resources.ThemeDictionaries["Light"]; var color = (SolidColorBrush)res["MainBackground"]; UploadArea.Background = color; UploadAreaHold.Visibility = Visibility.Visible; // 恢复默认上传信息 UploadInfoCollection.Clear(); UploadInfoCollection.Add(new KeyValue(AppTools.GetReswLanguage("DefaultUploadInfoTitle"), AppTools.GetReswLanguage("None"))); UploadInfoCollection.Add(new KeyValue(AppTools.GetReswLanguage("ImageSize"), AppTools.GetReswLanguage("None"))); UploadInfoCollection.Add(new KeyValue(AppTools.GetReswLanguage("ImageType"), AppTools.GetReswLanguage("None"))); UploadInfoCollection.Add(new KeyValue(AppTools.GetReswLanguage("CreateDate"), AppTools.GetReswLanguage("None"))); UploadInfoWidth = uploadColumnWidth / UploadInfoCollection.Count; // 清除上传进度条 UploadProgressCollection.Clear(); // 当成功列表中有数据时,显示成功数目,并将成功上传的图片反映在UI上 if (imgItemList.Any()) { int num = await App.OneDriveTools.AddImageToDatabase(App.Db, imgItemList); string msg = AppTools.GetReswLanguage("AddImageSuccess").Replace("{count}", num.ToString()); var tipDialog = new TipDialog(AppTools.GetReswLanguage("AddSuccessTitle"), msg); await tipDialog.ShowAsync(); AppTools.WriteLocalSetting(AppSettings.SelectGroupIndex, groupDialog.SelectGroup.GroupId); GroupInit(groupDialog.SelectGroup); } // 如果错误列表中有数据,则提醒用户 if (errorFile.Any()) { int num = errorFile.Count; string msg = AppTools.GetReswLanguage("AddImageFailed").Replace("{count}", num.ToString()); var tipDialog = new TipDialog(AppTools.GetReswLanguage("AddFailTitle"), msg); await tipDialog.ShowAsync(); } } }; await groupDialog.ShowAsync(); } }