private async Task <ApiResponse> UploadImage(MultipartFormDataContent form) { var client = BsdHttpClient.Instance(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); HttpResponseMessage uploadResponse = await client.PostAsync(ApiConfig.CloudStorageApi + "/api/files/upload?folder=internalpostitem", form); ApiResponse uploadResonse = JsonConvert.DeserializeObject <ApiResponse>(await uploadResponse.Content.ReadAsStringAsync()); return(uploadResonse); }
private async Task <ApiResponse> CopyImage(string Images) { var client = BsdHttpClient.Instance(); HttpResponseMessage uploadResponse = await client.GetAsync(ApiConfig.CloudStorageApi + "/api/files/copy?fromfolder=liquidation&tofolder=liquidation_today&filenames=" + Images); if (uploadResponse.IsSuccessStatusCode) { ApiResponse apiResponse = JsonConvert.DeserializeObject <ApiResponse>(await uploadResponse.Content.ReadAsStringAsync()); return(apiResponse); } return(null); }
public async void DownloadGoogleSheetTemplate_Clicked(object sender, EventArgs a) { gridLoading.IsVisible = true; try { PermissionStatus storagePermission = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Storage); if (storagePermission != PermissionStatus.Granted) { if (await CrossPermissions.Current.ShouldShowRequestPermissionRationaleAsync(Permission.Storage)) { await DisplayAlert("Quyền truy cập", "Sim Here cần quyền truy cập vào bộ nhớ để lưu template", "Đồng ý"); } if (Device.RuntimePlatform == Device.iOS) { storagePermission = await PermissionHelper.CheckPermissions(Permission.Storage, "Quyền truy cập bộ nhớ", "Sim Here cần quyền truy cập vào bộ nhớ để lưu template"); } else { var results = await CrossPermissions.Current.RequestPermissionsAsync(Permission.Storage); storagePermission = results[Permission.Storage]; } } if (storagePermission == PermissionStatus.Granted) { HttpClient client = BsdHttpClient.Instance(); HttpResponseMessage response = await client.GetAsync(Configuration.AppConfig.API_IP + "import_template.xlsx", HttpCompletionOption.ResponseHeadersRead); var streamToReadFrom = await response.Content.ReadAsByteArrayAsync(); var service = DependencyService.Get <IFileService>(); string folderName = Device.RuntimePlatform == Device.iOS ? "" : "Download/SimHere"; service.SaveFile("import_template.xlsx", streamToReadFrom, folderName); if (Device.RuntimePlatform == Device.iOS) { service.OpenFile("import_template.xlsx", folderName); } else { XFToast.ShortMessage("Tải template thành công"); } } } catch { await DisplayAlert("", "Tải thất bại", "Đóng"); } gridLoading.IsVisible = false; }
// update avatar to sql server and mongodb. async void UpdateAvatar(Plugin.Media.Abstractions.MediaFile file, string fileName) { if (file == null) { return; } viewModel.IsLoading = true; StreamContent content = new StreamContent(file.GetStream()); content.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data") { Name = "files", FileName = fileName }; MultipartFormDataContent form = new MultipartFormDataContent(); form.Add(content); var apiResponse = await BsdHttpClient.Instance().PostAsync(ApiRouter.USER_AVATAR_UPLOAD, form); if (apiResponse.IsSuccessStatusCode) { string AvatarUrl = "avatar/" + fileName; ApiResponse response = await ApiHelper.Post(ApiRouter.USER_AVATAR_UPDATE, AvatarUrl, true, false); if (response.IsSuccess) { viewModel.User.AvatarUrl = AvatarUrl; string AvatarFullUrl = viewModel.User.AvatarFullUrl; image.Source = AvatarFullUrl; UserLogged.SaveAvatar(AvatarFullUrl); MessagingCenter.Send <ProfilePage, string>(this, "UpdateAvatar", AvatarUrl); await DisplayAlert("", Language.cap_nhat_anh_dai_dien_thanh_cong, Language.dong); } else { await DisplayAlert("", Language.khong_cap_nhat_duoc_anh_dai_dien, Language.dong); } } else { await DisplayAlert("", Language.khong_cap_nhat_duoc_anh_dai_dien, Language.dong); } viewModel.IsLoading = false; }
private async void SelectImage_Clicked(object sender, EventArgs e) { var actionSheet = await DisplayActionSheet("Tuỳ chọn", "Đóng", null, "Chụp ảnh", "Chọn ảnh"); Plugin.Media.Abstractions.MediaFile file = null; if (actionSheet == "Chụp ảnh") { await CrossMedia.Current.Initialize(); if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported) { await DisplayAlert("Máy ảnh", "Máy ảnh không khả dụng", "Đóng"); gridLoading.IsVisible = false; return; } PermissionStatus cameraStatus = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Camera); if (cameraStatus != PermissionStatus.Granted) { if (await CrossPermissions.Current.ShouldShowRequestPermissionRationaleAsync(Permission.Camera)) { await DisplayAlert("Quyền truy cập máy ảnh", "Sim ,ere cần truy cập vào máy ảnh để chụp hình cho ảnh đại diện", "Đồng ý"); } if (Device.RuntimePlatform == Device.iOS) { cameraStatus = await PermissionHelper.CheckPermissions(Permission.Camera, "Quyền truy cập máy ảnh", "Sim Here cần truy cập vào máy ảnh để chụp hình cho ảnh đại diện"); } else { var results = await CrossPermissions.Current.RequestPermissionsAsync(Permission.Camera); cameraStatus = results[Permission.Camera]; } } if (cameraStatus == PermissionStatus.Granted) { gridLoading.IsVisible = true; file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions { SaveToAlbum = false, PhotoSize = Plugin.Media.Abstractions.PhotoSize.MaxWidthHeight, MaxWidthHeight = 300, }); } } else if (actionSheet == "Chọn ảnh") { await CrossMedia.Current.Initialize(); Permission pickPhotoStatus = Permission.Storage; if (Device.RuntimePlatform == Device.iOS) { pickPhotoStatus = Permission.Photos; } PermissionStatus photoPermisstionStatue = await CrossPermissions.Current.CheckPermissionStatusAsync(pickPhotoStatus); if (photoPermisstionStatue != PermissionStatus.Granted) { if (await CrossPermissions.Current.ShouldShowRequestPermissionRationaleAsync(pickPhotoStatus)) { await DisplayAlert("Quyền truy cập hình ảnh", "Sim Here cần truy cập vào thư viện hình ảnh để đặt anh đại ", "Đồng ý"); } if (Device.RuntimePlatform == Device.iOS) { photoPermisstionStatue = await PermissionHelper.CheckPermissions(pickPhotoStatus, "Quyền truy cập hình ảnh", "Sim Here muốn truy cập vào thư viện hình ảnh của bạn để lấy hình ảnh đặt làm ảnh đại diện cho bạn."); } else { var results = await CrossPermissions.Current.RequestPermissionsAsync(pickPhotoStatus); photoPermisstionStatue = results[pickPhotoStatus]; } } if (photoPermisstionStatue == PermissionStatus.Granted) { gridLoading.IsVisible = true; file = await CrossMedia.Current.PickPhotoAsync(new Plugin.Media.Abstractions.PickMediaOptions() { MaxWidthHeight = 300, PhotoSize = Plugin.Media.Abstractions.PhotoSize.MaxWidthHeight }); } } else { gridLoading.IsVisible = false; } if (file == null) { gridLoading.IsVisible = false; return; } var client = BsdHttpClient.Instance(); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", UserLogged.AccessToken); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); MultipartFormDataContent form = new MultipartFormDataContent(); var stream = file.GetStream(); var content = new StreamContent(stream); content.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data") { Name = "files", FileName = "test.jpg" }; form.Add(content); try { HttpResponseMessage response = await(client.PostAsync("api/user/changeavatar", form)); string body = await response.Content.ReadAsStringAsync(); ApiResponse apiResponse = JsonConvert.DeserializeObject <ApiResponse>(body); if (apiResponse.IsSuccess) { string newFileName = apiResponse.Content.ToString(); ImageSource newImageSource = ImageSource.FromStream(() => file.GetStream()); SetAvatar(newImageSource); TrackProfileChange.AvatarHasChanged = true; UserLogged.AvatarUrl = AppConfig.API_IP + "Upload/Avatar/" + newFileName; XFToast.ShortMessage("Cập nhật thành cộng"); gridLoading.IsVisible = false; } else { await DisplayAlert("", "Không thể cập nhật ảnh đại diện", "Đóng"); gridLoading.IsVisible = false; } } catch (Exception ex) { Console.WriteLine(ex.Message); gridLoading.IsVisible = false; } }
private async void Save_Clicked(object sender, EventArgs e) { if (viewModel.ParentCategory == null) { await DisplayAlert("", Language.chon_nhom_danh_muc, Language.dong); return; } if (viewModel.ChildCategory == null) { await DisplayAlert("", Language.chon_danh_muc, Language.dong); return; } if (string.IsNullOrWhiteSpace(viewModel.AddProductModel.Name)) { await DisplayAlert("", Language.nhap_ten_noi_that, Language.dong); return; } if (!viewModel.AddProductModel.Price.HasValue) { await DisplayAlert("", Language.nhap_gia_san_pham, Language.dong); return; } if (viewModel.AddProductModel.IsPromotion == true) { if (viewModel.AddProductModel.PromotionFromDate == null) { await DisplayAlert("", Language.vui_long_chon_thoi_gian, Language.dong); return; } if (viewModel.AddProductModel.PromotionToDate == null) { await DisplayAlert("", Language.vui_long_chon_thoi_gian, Language.dong); return; } if (viewModel.AddProductModel.PromotionPrice == null) { await DisplayAlert("", Language.vui_long_nhap_gia_giam, Language.dong); return; } } if (viewModel.AddProductModel.Province == null) { await DisplayAlert("", Language.vui_long_chon_tinh_thanh, Language.dong); return; } if (viewModel.AddProductModel.District == null) { await DisplayAlert("", Language.vui_long_chon_quan_huyen, Language.dong); return; } if (viewModel.AddProductModel.Ward == null) { await DisplayAlert("", Language.vui_long_chon_phuong_xa, Language.dong); return; } if (viewModel.Media.Count < 1) { await DisplayAlert("", Language.chon_hinh_anh_cho_san_pham, Language.dong); return; } loadingPopup.IsVisible = true; FurnitureProduct product = new FurnitureProduct(); product.Name = viewModel.AddProductModel.Name; product.ParentCategoryId = viewModel.ParentCategory.Id; product.CategoryId = viewModel.ChildCategory.Id; product.CreatedById = Guid.Parse(UserLogged.Id); product.Price = viewModel.AddProductModel.Price; product.Status = viewModel.AddProductModel.Status; product.Model = viewModel.AddProductModel.Model; product.Origin = viewModel.AddProductModel.Origin; product.Guarantee = viewModel.AddProductModel.Guarantee; product.Description = viewModel.AddProductModel.Description; product.CreatedDate = viewModel.AddProductModel.CreatedDate; product.ProductStatus = 0; product.IsPromotion = viewModel.AddProductModel.IsPromotion; if (viewModel.AddProductModel.IsPromotion == true) { product.PromotionFromDate = viewModel.AddProductModel.PromotionFromDate; product.PromotionToDate = viewModel.AddProductModel.PromotionToDate; product.PromotionPrice = viewModel.AddProductModel.PromotionPrice; } if (viewModel.AddProductModel.Province != null) { product.ProvinceId = viewModel.AddProductModel.Province.Id; } if (viewModel.AddProductModel.District != null) { product.DistrictId = viewModel.AddProductModel.District.Id; } if (viewModel.AddProductModel.Ward != null) { product.WardId = viewModel.AddProductModel.Ward.Id; } product.Street = viewModel.AddProductModel.Street; product.Address = viewModel.AddProductModel.Address; string[] imageList = new string[viewModel.Media.Count]; for (int i = 0; i < viewModel.Media.Count; i++) { var item = viewModel.Media[i]; // chua upload. upload roi link = null if (string.IsNullOrEmpty(item.Path) == false) // co link la co chon tu dien thoai. { imageList[i] = $"{Guid.NewGuid().ToString()}.jpg"; var stream = new MemoryStream(File.ReadAllBytes(item.Path)); var content = new StreamContent(stream); content.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data") { Name = "files" + i, FileName = imageList[i] }; MultipartFormDataContent form = new MultipartFormDataContent(); form.Add(content); var response = await BsdHttpClient.Instance().PostAsync(ApiRouter.FURNITUREPRODUCT_IMAGE_UPLOAD, form); if (!response.IsSuccessStatusCode) { await DisplayAlert("", Language.khong_the_upload_hinh_anh, Language.dong); loadingPopup.IsVisible = false; return; } } else { imageList[i] = item.PreviewPath.Replace(Configuration.ApiConfig.CloudStorageApiCDN + "/furniture/product/", ""); } } product.AvatarUrl = imageList[0]; product.Images = string.Join(",", imageList); if (chkCompanyAddress.IsChecked && viewModel.Company != null) //sp cty { product.CompanyId = viewModel.Company.Id; } //save ApiResponse apiResponse = null; if (_productId == Guid.Empty)//add { apiResponse = await ApiHelper.Post(ApiRouter.FURNITUREPRODUCT_ADD_UPDATE, product, true); } else//upd { product.Id = _productId; apiResponse = await ApiHelper.Put($"{ApiRouter.FURNITUREPRODUCT_ADD_UPDATE}/update", product, true); } if (apiResponse.IsSuccess) { var responseProduct = JsonConvert.DeserializeObject <FurnitureProduct>(apiResponse.Content.ToString()); product.Id = responseProduct.Id; await Navigation.PopAsync(); if (_productId == Guid.Empty) { MessagingCenter.Send <AddProductPage, bool>(this, "AddProduct", (bool)viewModel.AddProductModel.IsPromotion); ToastMessageHelper.ShortMessage(Language.dang_thanh_cong); } else { MessagingCenter.Send <AddProductPage, FurnitureProduct>(this, "UpdateProduct", product); ToastMessageHelper.ShortMessage(Language.luu_san_pham_thanh_cong); } } else { ToastMessageHelper.ShortMessage($"{Language.loi}, {apiResponse.Message}"); } loadingPopup.IsVisible = false; }