Image sent to my webservice is always null using Carter framework

103 Views Asked by At

I am having a small issue where the image im sending from postman is not populated into my model when using BindAndValidate method.

Here is my model :

public class Photo {
     public string date {get; set;}
     public byte[] image {get; set;} 
}

And here is my actual call :

curl -k -X POST \
 http://localhost:8888/upload \
 -H 'Content-Type: multipart/form-data' \
 -H 'cache-control: no-cache' \
 -H 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \
 -F 'image=@\\LAPTOP\USERDAT01\MATIAS\Desktop\image.jpg' \
 -F date=2019-10-16

The problem im facing is that although date field on my model is actually getting populated the image property is always null.

This is my module code :

var request = req.BindAndValidate<Photo>();
uploadPhoto(request.data.date, request.data.image)

What am I doing wrong?

1

There are 1 best solutions below

0
On

I finally solved it as :

var request = req.BindAndValidate<Photo>();
using (StreamReader reader = new StreamReader(req.BindFile().Result.OpenReadStream()))
{
      request.Data.image = Encoding.ASCII.GetBytes(reader.ReadToEnd());
 }

Thanks!