1

I am trying to send an image (StreamImageSource) to my backend but I recieve this error:

A Newtonsoft.Json.JsonSerializationException was thrown.

Self referencing loop detected for property 'Member' with type 'System.Reflection.MonoMethod'. Path 'photoData.Stream.Method.ReturnParameter'.

Is this related to my backend or has the error something to do with the way I am trying to send it?

This is what I am attempting to send from my contentpage, "imgPicked" is an image that gives me the value "Xamarin.Forms.StreamImageSource" when I run it in the the log:

async void createImage (object s, EventArgs a)
    {
        System.Diagnostics.Debug.WriteLine (imgPicked.Source);
        //imgPicked.Source is a StreamImageSource
        var create = await phpApi.createPhotoTwo (imgPicked.Source);
    }

How I try to send it in:

static public async Task <bool>  createPhotoTwo (ImageSource imgData)
    {   
        var httpClientRequest = new HttpClient ();

        var postData = new Dictionary <string, object> ();

        postData.Add ("photoData", imgData);

        var jsonRequest = JsonConvert.SerializeObject(postData);

        HttpContent content = new StringContent(jsonRequest, System.Text.Encoding.UTF8, "application/json");

        var result = await httpClientRequest.PostAsync("http://www.myadress.com/put.php", content);
        var resultString = await result.Content.ReadAsStringAsync ();

        return  true;

    }
medvedo
  • 543
  • 2
  • 8
  • 23
  • as explained (repeatedly) in the past, you need to send your image as a byte array – Jason Jun 21 '16 at 18:32
  • so a StreamImageSource is not going to work then? I got the help with getting the StreamImageSource by a guy from this post: http://stackoverflow.com/questions/37946391/how-do-i-reach-the-mediafile-on-my-viewmodel-and-use-it-on-my-contentpage – medvedo Jun 21 '16 at 18:34
  • that question was asking how to pass it from your VM to your View – Jason Jun 21 '16 at 18:35
  • Ah ok. I did not ask the correct question then. What do you think I should do? Create a new thread explaining more in depth about how to get the value from the byte and send that to the db? I have created 2-3 topics about this but I never seem to figure it out unfortunately. – medvedo Jun 21 '16 at 18:37
  • How should I refer the question? – medvedo Jun 21 '16 at 18:44
  • I suggest you read some of the old questions where this has been explained to you instead of asking the same question again, which is frowned up on SO. If you really don't understand it, then you might be better off asking on the Xamarin forums, which are more suited for open-ended discussion. – Jason Jun 21 '16 at 18:46
  • It is correct that you have told me about byte [] but u have not posted any code showing how I can reach the byte []. And after my last post I thought that a streamimagesource could work as well but apparently they misunderstood my question/I asked a bad question – medvedo Jun 21 '16 at 19:00
  • http://stackoverflow.com/questions/221925/creating-a-byte-array-from-a-stream – Jason Jun 21 '16 at 19:05

1 Answers1

0

Is this related to my backend or has the error something to do with the way I am trying to send it?

It is because Newtonsoft doesn't know how to serialize your StreamImageSource object. I recommend upload your image this way: C# HttpClient 4.5 multipart/form-data upload

Community
  • 1
  • 1
Santiago Hernández
  • 5,438
  • 2
  • 26
  • 34
  • Trying to adjust my code but the posts on the link you gave only seem to work with byte [] and not StreamImageSource like I currently work with – medvedo Jun 21 '16 at 18:32