Пример #1
0
        public AddEnrollPage()
        {
            InitializeComponent();
            this.Title = AppResources.AddEnroll;

            edtRemark.Text      = AppResources.Remark;
            edtRemark.TextColor = Color.Gray;

            btnSubmit.Clicked += async(sender, args) =>
            {
                if (etyName.Text == "")
                {
                    await DisplayAlert(AppResources.Error, AppResources.NoUserName, AppResources.OK);

                    return;
                }

                if (etyTelephone.Text == "")
                {
                    await DisplayAlert(AppResources.Error, AppResources.NoTelephoneNumber, AppResources.OK);

                    return;
                }

                IsBusy = true;
                EnrollResponseResult enrollResponseResult = await App.EnrollManager.Create(
                    App.mUser.ID,
                    etyName.Text.Trim(),
                    etyTelephone.Text.Trim(),
                    edtRemark.Text.Trim(),
                    mfUploadImage);

                IsBusy = false;

                if (enrollResponseResult.Success == true)
                {
                    await Navigation.PopAsync();
                }
                else
                {
                    await DisplayAlert(AppResources.Error, enrollResponseResult.Error, AppResources.OK);
                }
            };
        }
Пример #2
0
        async Task <EnrollResponseResult> IEnrollRestService.Create(int senderUserID, string name, string telephone, string remark, MediaFile file)
        {
            EnrollResponseResult rr = new EnrollResponseResult();
            var url = Constants.host + "/api/enroll/create";
            var uri = new Uri(url);

            try
            {
                // Get Orientation
                int rotate = 0;

#if __ANDROID__
                // Android-specific code

                var exifInterface = new ExifInterface(file.Path);
                int orientation   = exifInterface.GetAttributeInt(ExifInterface.TagOrientation, 0);
                switch (orientation)
                {
                case (int)Orientation.Rotate270:
                    rotate = 90;
                    break;

                case (int)Orientation.Rotate180:
                    rotate = 180;
                    break;

                case (int)Orientation.Rotate90:
                    rotate = 270;
                    break;
                }
#endif
                //read file into upfilebytes array
                byte[] upfilebytes = null;
                using (var memoryStream = new MemoryStream())
                {
                    file.GetStream().CopyTo(memoryStream);
                    file.Dispose();
                    upfilebytes = memoryStream.ToArray();
                }

                // Convert byte[] to Base64 String
                string base64String = Convert.ToBase64String(upfilebytes);

                //create new HttpClient and MultipartFormDataContent and add our file
                MultipartFormDataContent content            = new MultipartFormDataContent();
                StringContent            scSenderUserID     = new StringContent(senderUserID.ToString());
                StringContent            scName             = new StringContent(name.ToString());
                StringContent            scTelephone        = new StringContent(telephone.ToString());
                StringContent            scRemark           = new StringContent(remark.ToString());
                StringContent            scUploadImage      = new StringContent(base64String);
                StringContent            scImageOrientation = new StringContent(rotate.ToString());
                content.Add(scSenderUserID, "sender_user_id");
                content.Add(scName, "name");
                content.Add(scTelephone, "telephone");
                content.Add(scRemark, "remark");
                content.Add(scUploadImage, "str_image");
                content.Add(scImageOrientation, "img_orientation");

                var response = await client.PostAsync(uri, content);

                if (response.IsSuccessStatusCode)
                {
                    var result = await response.Content.ReadAsStringAsync();

                    rr = JsonConvert.DeserializeObject <EnrollResponseResult>(result);

                    Debug.WriteLine(@"				Enroll insert successfully.");
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(@"				ERROR {0}", ex.Message);
            }

            return(rr);
        }