0

I want to save images into my mysql database.

This is my code:

The class:

    public async void SIE()
    {
        TrialClass trialClass = new TrialClass(ImagesPaths1);
        BlogRestClient<TrialClass> restClient = new BlogRestClient<TrialClass>();
        await restClient.PostAsync(trialClass);
    }

    public class TrialClass
    {
        public List<ImageFormatClass> ImagesPath2;

        public TrialClass(List<ImageFormatClass> imagespath)
        {
            ImagesPath2 = imagespath;
        }
    }

    public class ImageFormatClass
    {
        public Image SavedImage;
        public int Format;

        public ImageFormatClass()
        {
        }
    }

    private void Post_Clicked(object sender, EventArgs e)
    {
            SIE();
    }

Web API controller:

public void Post([FromBody] TrialClass value)
{
        foreach (ImageFormatClass s in value.ImagesPath2)
        {
            string sqlstring = "server=; port= ; user id =;Password=;Database=;";
            MySqlConnection conn = new MySqlConnection(sqlstring);

            try
            {
                conn.Open();
            }
            catch (MySqlException ex)
            {
                throw ex;
            }

            string Query = "INSERT INTO test.blogimagestable (ImagesId)values( ?str);";

            MySqlCommand cmd = new MySqlCommand(Query, conn);
            cmd.Parameters.Add("?str", MySqlDbType.VarString, 256).Value = s.SavedImage;

            cmd.ExecuteReader();

            conn.Close();
        }
}

Anytime I press the post button to fire the Post_Clicked event, I get this error

Newtonsoft.Json.JsonSerializationException: 'Self referencing loop detected for property 'ManifestModule' with type 'System.Reflection.RuntimeModule'. Path 'ImagesPath2[0].SavedImage.Source.Stream.Method.Module.Assembly'.'

at `await restClient.PostAsync(trialClass);

7
  • stackoverflow.com/questions/13510204/… Commented Jul 15, 2019 at 16:24
  • What is Image? Xamarin.Forms.Image is a UI element, not bitmap data that you can transfer over the wire. Commented Jul 15, 2019 at 16:43
  • @Jason So what should I do? Commented Jul 15, 2019 at 16:45
  • you need to use the underlying image data was was used to create the Image's ImageSource Commented Jul 15, 2019 at 16:46
  • 1
    Let's assume you're getting the image from a file. Then all you need to do is read that file as a byte[] and send it to your service. Commented Jul 15, 2019 at 21:48

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.