Post byte array to Web API server using HttpClient

后端 未结 4 1590
执念已碎
执念已碎 2020-11-30 07:14

I want to post this data to Web API server:

public sealed class SomePostRequest
{
    public int Id { get; set; }
    public byte[] Content { get; set; }
}
<         


        
4条回答
  •  自闭症患者
    2020-11-30 08:07

    I convert Byte Array into Base64 String to post:

    await client.PostAsJsonAsync( apiUrl,  
        new  {
            message = "",
            content = Convert.ToBase64String(yourByteArray),
        }
    );
    

    and receiver can convert the Base64 String back to Byte Array by:

    string base64Str = (string)postBody.content;
    byte[] fileBytes = Convert.FromBase64String(base64Str);
    

提交回复
热议问题