0

I'm trying to store a model informations in my Data Base, this informations contain an image, so I created a simple view with some entrys, with a media picker button to pick it and an image witch display the picked one. This is the button method and the store method:

 async void Button_Clicked(System.Object sender, System.EventArgs e)
        {
            var result = await MediaPicker.PickPhotoAsync(new MediaPickerOptions
            {
                Title = "Please pick a photo"
            });

            if (result != null)
            {
                var stream = await result.OpenReadAsync();

                resultImage.Source = ImageSource.FromStream(() => stream);
            }
        }
  public async Task SaveMachine()

        {

            var machine = new Machine
            {
                Machine_Name = nom.Text,
                Machine_Qr = qr.Text,
                Files = resultImage

            };

            await _rest.AddMachine(machine);

            await Shell.Current.GoToAsync("..");
        }

But I can't create Files = resultImage because the Files in the model is in IFormFile.

Gazdallah Amira
  • 188
  • 3
  • 16
  • Save the `IFormFile` to your disk instead, and save the path to the file in the database. – nbokmans Jun 17 '22 at 12:28
  • I used asp.net core and I created an api to store both the image and the image path in the sqlServer – Gazdallah Amira Jun 17 '22 at 12:30
  • @Jason can you have a look ? – Gazdallah Amira Jun 17 '22 at 12:40
  • `resultImage` is an **Image control**, not image data. You need to send the data to the server. Based on the code you posted I'm not sure what the best way to do that is. – Jason Jun 17 '22 at 13:19
  • no the resultImage is the name of the image in the view – Gazdallah Amira Jun 17 '22 at 13:49
  • 1
    it is the name of the **Image control** in your UI. An `Entry` is a UI control that contains a string, it is not the string itself. Likewise, an `Image` is a UI control that contains image data, but it is not the data itself. – Jason Jun 17 '22 at 13:51
  • if `Files` is an `IFormFile`, then it has a constructor that takes a stream. You would use the same stream used to create the `Image` to create the `FormFile` – Jason Jun 17 '22 at 13:52
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/245692/discussion-between-gazdallah-amira-and-jason). – Gazdallah Amira Jun 17 '22 at 14:00
  • keep a reference to the stream (or a byte[] of the data) when you select the image, then use that reference again when creating the FormFile – Jason Jun 17 '22 at 14:25
  • I didn't inderstand what you mean by "keep a reference to the stream (or a byte[] of the data)", can you more explain ? – Gazdallah Amira Jun 17 '22 at 18:41
  • The `Files` and `resultImage` are different type of variable, so you can try to get the instance of `Files` from a stream, such as this [link](https://stackoverflow.com/questions/71477717/how-to-create-an-iformfile-image-file-and-send-it-to-input-element-in-view). – Liyun Zhang - MSFT Jun 21 '22 at 06:51

0 Answers0