public AttachmentPage(ObservationAttachment attachment = null) { InitializeComponent(); buttonLayout.BackgroundColor = Color.FromHex("#034468"); NavigationPage.SetBackButtonTitle(this, "Cancel"); if (attachment == null) { _attachment = new ObservationAttachment { Bytes = null }; } else { _attachment = attachment; } ViewModel.Attachment = _attachment; }
public JsonResult AddAttachment([Bind(Exclude = "FileBytes")] ObservationAttachment model, HttpPostedFileBase fileBytes) { using (Entity context = new Entity()) { try { t_observation_attachment attachment = new t_observation_attachment(); attachment.observation_id = model.ObservationId; attachment.attachment_file_name = model.FileName; byte[] file = new byte[fileBytes.ContentLength]; int result = fileBytes.InputStream.Read(file, 0, fileBytes.ContentLength); attachment.attachment = file; attachment.createdby_userid = CurrentUser.Id; attachment.created_date = DateTime.Now; context.t_observation_attachment.Add(attachment); context.SaveChanges(); context.Entry(attachment).Reference(n => n.createdby).Load(); return(Json(new { success = true, AttachmentId = attachment.observation_attachment_id, ObservationId = attachment.observation_id, FileName = attachment.attachment_file_name, FileSize = attachment.attachment.Length, Approved = attachment.active, CreatedBy = attachment.createdby.full_name, CreatedOn = attachment.created_date.ToString("d") })); } catch (Exception ex) { return(GetJsonResult(ex)); } } }
async void CameraButtonClicked(object sender, System.EventArgs e) { //taken from https://github.com/jamesmontemagno/MediaPlugin if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported) { await App.CurrentApp.MainPage.DisplayAlert("No Camera", "No camera available.", "OK"); return; } var filename = DateTime.Now.ToString("yyyy-dd-M--HH-MM-ss") + ".jpg"; var file = await CrossMedia.Current.TakePhotoAsync(new StoreCameraMediaOptions { Directory = "USAID", Name = filename, PhotoSize = PhotoSize.Small }); if (file == null) { return; } if (ViewModel.Attachments == null) { ViewModel.Attachments = new ObservableCollection <ObservationAttachment>(); } //var image = new Image(); //image.Source = ImageSource.FromStream(() => //{ // var stream = file.GetStream(); // file.Dispose(); // return stream; //}); System.IO.Stream stream = file.GetStream(); stream.Position = 0; byte[] buffer = new byte[stream.Length]; for (int totalBytesCopied = 0; totalBytesCopied < stream.Length;) { totalBytesCopied += stream.Read(buffer, totalBytesCopied, Convert.ToInt32(stream.Length) - totalBytesCopied); } var attachment = new ObservationAttachment { Bytes = buffer, Attachment_File_Name = filename, Created = DateTime.Now, Created_Date = DateTime.Now }; //MessagingCenter.Send(attachment, "AttachmentScreen"); if (ViewModel.Attachments == null) { ViewModel.Attachments = new ObservableCollection <ObservationAttachment>(); } var item = ViewModel.Attachments.Where(m => m == attachment).FirstOrDefault(); if (item != null) { ViewModel.Attachments.Remove(item); ViewModel.Attachments.Add(item); } else { ViewModel.Attachments.Add(attachment); } changeListHeights(); }
public async void OnAttachmentsItemTapped(object sender, ItemTappedEventArgs e) { ObservationAttachment attach = (ObservationAttachment)e.Item; ////await Navigation.PushAsync(new ObservationPage(indicator, _site)); await Navigation.PushModalAsync(new AttachmentPage(attach)); }