CarterCommunity / Carter

Carter is framework that is a thin layer of extension methods and functionality over ASP.NET Core allowing code to be more explicit and most importantly more enjoyable.
MIT License
2.08k stars 175 forks source link

Sending image as multipart post request #208

Closed Matias-Barrios closed 4 years ago

Matias-Barrios commented 4 years ago

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?

jchannon commented 4 years ago

Nothing I’m not sure we have that scenario supported.

We do have a BindFile APIs that you could look at.

You would need to call that and Bind to get your Date field populated

On Wed, 16 Oct 2019 at 19:09, Matias.Barrios notifications@github.com wrote:

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();uploadPhoto(request.data.date, request.data.image)

What am I doing wrong?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/CarterCommunity/Carter/issues/208?email_source=notifications&email_token=AAAZVJW4DN2Q7X7JW43OHJ3QO5KGJA5CNFSM4JBPCY6KYY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4HSHVJNA, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAZVJRMUBFBT2NCBPHULSLQO5KGJANCNFSM4JBPCY6A .

Matias-Barrios commented 4 years ago

Thanks so much for your comment, it helped me to get this. I used BindFile to get the actual image and BindAndValidate for the body.

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

Thanks!

jchannon commented 4 years ago

Would recommend making your route handler async to avoid the .Result :)