簡體   English   中英

將圖像(StreamImageSource)發送到Xamarin Forms的后端

[英]Sending an image (StreamImageSource) to a backend in Xamarin Forms

我正在嘗試將圖像(StreamImageSource)發送到后端,但是我收到此錯誤:

A Newtonsoft.Json.JsonSerializationException was thrown.

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

這與我的后端有關還是錯誤與我嘗試發送的方式有關?

這是我嘗試從內容頁發送的內容,“ imgPicked”是一張圖像,當我在日志中運行它時,該圖像為我提供值“ Xamarin.Forms.StreamImageSource”:

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);
    }

我如何發送它:

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;

    }

這與我的后端有關還是錯誤與我嘗試發送的方式有關?

這是因為Newtonsoft不知道如何序列化StreamImageSource對象。 我建議以這種方式上傳您的圖片: C#HttpClient 4.5 multipart / form-data upload

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM