private async void AddAttachment_Click(object sender, EventArgs e) { if (_selectedFeature == null) { return; } // Adjust the UI. AddAttachmentButton.IsEnabled = false; AttachmentActivityIndicator.IsVisible = true; // Get the file. string contentType = "image/jpeg"; try { byte[] attachmentData; string filename; // Xamarin.Plugin.FilePicker shows the iCloud picker (not photo picker) on iOS. // This iOS code shows the photo picker. #if __IOS__ Stream imageStream = await GetImageStreamAsync(); if (imageStream == null) { return; } attachmentData = new byte[imageStream.Length]; imageStream.Read(attachmentData, 0, attachmentData.Length); filename = _filename ?? "file1.jpeg"; #else // Show a file picker - this uses the Xamarin.Plugin.FilePicker NuGet package. FileData fileData = await CrossFilePicker.Current.PickFile(new[] { ".jpg", ".jpeg" }); if (fileData == null) { return; } if (!fileData.FileName.EndsWith(".jpg") && !fileData.FileName.EndsWith(".jpeg")) { await Application.Current.MainPage.DisplayAlert("Try again!", "This sample only allows uploading jpg files.", "OK"); return; } attachmentData = fileData.DataArray; filename = fileData.FileName; #endif // Add the attachment. // The contentType string is the MIME type for JPEG files, image/jpeg. await _selectedFeature.AddAttachmentAsync(filename, contentType, attachmentData); // Get a reference to the feature's service feature table. ServiceFeatureTable serviceTable = (ServiceFeatureTable)_selectedFeature.FeatureTable; // Apply the edits to the service feature table. await serviceTable.ApplyEditsAsync(); // Update UI. _selectedFeature.Refresh(); AttachmentsListBox.ItemsSource = await _selectedFeature.GetAttachmentsAsync(); await Application.Current.MainPage.DisplayAlert("Success!", "Successfully added attachment", "OK"); } catch (Exception exception) { await Application.Current.MainPage.DisplayAlert("Error adding attachment", exception.ToString(), "OK"); } finally { // Adjust the UI. AddAttachmentButton.IsEnabled = true; AttachmentActivityIndicator.IsVisible = false; } }
private async void AddAttachment_Click(object sender, RoutedEventArgs e) { if (_selectedFeature == null) { return; } // Adjust the UI. AddAttachmentButton.IsEnabled = false; ActivityIndicator.Visibility = Visibility.Visible; // Get the file. string contentType = "image/jpeg"; byte[] attachmentData; try { // Show a file picker. // Allow the user to specify a file path - create the picker. FileOpenPicker openPicker = new FileOpenPicker(); openPicker.FileTypeFilter.Add(".jpg"); // Show the picker. StorageFile file = await openPicker.PickSingleFileAsync(); // Take action if the user selected a file. if (file == null) { return; } // Read the file contents into memory. Stream dataStream = await file.OpenStreamForReadAsync(); attachmentData = new byte[dataStream.Length]; dataStream.Read(attachmentData, 0, attachmentData.Length); dataStream.Close(); // Add the attachment. // The contentType string is the MIME type for JPEG files, image/jpeg. await _selectedFeature.AddAttachmentAsync(file.Name, contentType, attachmentData); // Get a reference to the feature's service feature table. ServiceFeatureTable serviceTable = (ServiceFeatureTable)_selectedFeature.FeatureTable; // Apply the edits to the service feature table. await serviceTable.ApplyEditsAsync(); // Update UI. _selectedFeature.Refresh(); AttachmentsListBox.ItemsSource = await _selectedFeature.GetAttachmentsAsync(); await new MessageDialog2("Successfully added attachment", "Success!").ShowAsync(); } catch (Exception exception) { await new MessageDialog2(exception.ToString(), "Error adding attachment").ShowAsync(); } finally { // Adjust the UI. AddAttachmentButton.IsEnabled = true; ActivityIndicator.Visibility = Visibility.Collapsed; } }
private async void AddAttachment_Click(object sender, RoutedEventArgs e) { if (_selectedFeature == null) { return; } // Adjust the UI. AddAttachmentButton.IsEnabled = false; ActivityIndicator.Visibility = Visibility.Visible; try { // Show a file dialog. // Allow the user to specify a file path - create the dialog. OpenFileDialog dlg = new OpenFileDialog { DefaultExt = ".jpg", Filter = "Image Files(*.JPG;*.JPEG)|*.JPG;*.JPEG", InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures) }; // Show the dialog and get the results. bool?result = dlg.ShowDialog(); // Take action if the user selected a file. if (result != true) { return; } // Get the name of the file from the full path (dlg.FileName is the full path). string filename = Path.GetFileName(dlg.FileName); // Create a stream for reading the file. FileStream fs = new FileStream(dlg.FileName, FileMode.Open, FileAccess.Read); // Create a binary reader from the stream. BinaryReader br = new BinaryReader(fs); // Populate the attachment data with the binary content. long numBytes = new FileInfo(dlg.FileName).Length; byte[] attachmentData = br.ReadBytes((int)numBytes); // Close the stream. fs.Close(); // Add the attachment. // The contentType string is the MIME type for JPEG files, image/jpeg. await _selectedFeature.AddAttachmentAsync(filename, "image/jpeg", attachmentData); // Get a reference to the feature's service feature table. ServiceFeatureTable serviceTable = (ServiceFeatureTable)_selectedFeature.FeatureTable; // Apply the edits to the service feature table. await serviceTable.ApplyEditsAsync(); // Update UI. _selectedFeature.Refresh(); AttachmentsListBox.ItemsSource = await _selectedFeature.GetAttachmentsAsync(); MessageBox.Show("Successfully added attachment", "Success!"); } catch (Exception exception) { MessageBox.Show(exception.ToString(), "Error adding attachment"); } finally { // Adjust the UI. AddAttachmentButton.IsEnabled = true; ActivityIndicator.Visibility = Visibility.Collapsed; } }