JsonModelBinder icon indicating copy to clipboard operation
JsonModelBinder copied to clipboard

model empty

Open nmicun opened this issue 4 years ago • 5 comments

Im trying with your solution but I have a problem. Model is always null. Only image file was read correctly. This is post request which is send from my video camera after license plate is recognized:

POST /alpv/receive.php HTTP/1.1\r\n Host: 10.13.24.13\r\n Content-Length: 7160\r\n Content-Type: multipart/form-data; boundary=------------------------3df4e8eee91da308\r\n --------------------------3df4e8eee91da308 Content-Disposition: form-data; name="event"; filename="20190204103017_10536event_7000175.json" Content-Type: application/octet-stream { "packetCounter":"7000175", "datetime":"20190204 103017000", "plateText":"\u004c\u0042\u0045\u0033\u0039\u0037", "plateCountry":"SWE", "plateConfidence":"0.716159", "carState":"new", "geotag":{"lat": 50.418114,"lon": 30.476213}, "imageType": "plate", "plateImageType": "png", "plateImageSize": "0", "carMoveDirection":"in", "timeProcessing":"0", "plateCoordinates":[1614, 880, 172, 28], "carID":"18", "GEOtarget":"Camera", "sensorProviderID":"defaultID" } --------------------------3df4e8eee91da308 Content-Disposition: form-data; name="image"; filename="20190204103016_999776lp_LBE397_7000175.png" Content-Type: application/octet-stream <IMAGE DATA> --------------------------3df4e8eee91da308--

There are two files: "image" and "event".

In controller I write this:

public IActionResult NewEvent([ModelBinder(BinderType = typeof(JsonModelBinder))] DataFromCameraModel @event, IFormFile image) { return Ok(); }

and DataFromCameraModel

public class DataFromCameraModel { public int packetCounter {get;set;} public string Datetime {get;set;} public string plateText {get;set;} }

nmicun avatar May 23 '20 11:05 nmicun

Im trying with your solution but I have a problem. Model is always null. Only image file was read correctly. This is post request which is send from my video camera after license plate is recognized:

POST /alpv/receive.php HTTP/1.1\r\n Host: 10.13.24.13\r\n Content-Length: 7160\r\n Content-Type: multipart/form-data; boundary=------------------------3df4e8eee91da308\r\n --------------------------3df4e8eee91da308 Content-Disposition: form-data; name="event"; filename="20190204103017_10536event_7000175.json" Content-Type: application/octet-stream { "packetCounter":"7000175", "datetime":"20190204 103017000", "plateText":"\u004c\u0042\u0045\u0033\u0039\u0037", "plateCountry":"SWE", "plateConfidence":"0.716159", "carState":"new", "geotag":{"lat": 50.418114,"lon": 30.476213}, "imageType": "plate", "plateImageType": "png", "plateImageSize": "0", "carMoveDirection":"in", "timeProcessing":"0", "plateCoordinates":[1614, 880, 172, 28], "carID":"18", "GEOtarget":"Camera", "sensorProviderID":"defaultID" } --------------------------3df4e8eee91da308 Content-Disposition: form-data; name="image"; filename="20190204103016_999776lp_LBE397_7000175.png" Content-Type: application/octet-stream <IMAGE DATA> --------------------------3df4e8eee91da308--

There are two files: "image" and "event".

In controller I write this:

public IActionResult NewEvent([ModelBinder(BinderType = typeof(JsonModelBinder))] DataFromCameraModel @event, IFormFile image) { return Ok(); }

and DataFromCameraModel

public class DataFromCameraModel { public int packetCounter {get;set;} public string Datetime {get;set;} public string plateText {get;set;} }

Hello. I have the same problem. Did you find a solution?

o-medizadeh-Ard avatar Mar 07 '22 16:03 o-medizadeh-Ard

I solved as follows:

[HttpPost("newevent")]
        [Consumes("multipart/form-data")]

        public IActionResult NewEvent(IFormFile @event, IFormFile image)
        {
            using (var reader = new StreamReader(@event.OpenReadStream()))
           {
             eventmodel = JsonConvert.DeserializeObject<DataFromCameraModel>(reader.ReadToEnd());
                
           }
            firstchar = eventmodel.plateText[0];

            if (eventmodel.plateText.Length == 7 && !Char.IsDigit(firstchar))
            {
                var imagepath = _truckService.UploadNewEventImage(image);
                _truckService.SaveNewEvent(eventmodel,imagepath);
                this.HubContext.Clients.All.SendAsync("Refresh", eventmodel);
            }
            return Ok();
        }


public class DataFromCameraModel
    {
       
   
        public string Datetime { get; set; }
        public string plateText { get; set; }
      

    }

nmicun avatar Mar 07 '22 16:03 nmicun

I solved as follows:

[HttpPost("newevent")]
        [Consumes("multipart/form-data")]

        public IActionResult NewEvent(IFormFile @event, IFormFile image)
        {
            using (var reader = new StreamReader(@event.OpenReadStream()))
           {
             eventmodel = JsonConvert.DeserializeObject<DataFromCameraModel>(reader.ReadToEnd());
                
           }
            firstchar = eventmodel.plateText[0];

            if (eventmodel.plateText.Length == 7 && !Char.IsDigit(firstchar))
            {
                var imagepath = _truckService.UploadNewEventImage(image);
                _truckService.SaveNewEvent(eventmodel,imagepath);
                this.HubContext.Clients.All.SendAsync("Refresh", eventmodel);
            }
            return Ok();
        }


public class DataFromCameraModel
    {
       
   
        public string Datetime { get; set; }
        public string plateText { get; set; }
      

    }

Thanks for the reply. So you did not actually use "JsonModelBinder"!

o-medizadeh-Ard avatar Mar 07 '22 16:03 o-medizadeh-Ard

didnt use, you can get data easily with this:

 using (var reader = new StreamReader(@event.OpenReadStream()))
           {
             eventmodel = JsonConvert.DeserializeObject<DataFromCameraModel>(reader.ReadToEnd());
                
           }

nmicun avatar Mar 07 '22 16:03 nmicun

didnt use, you can get data easily with this:

 using (var reader = new StreamReader(@event.OpenReadStream()))
           {
             eventmodel = JsonConvert.DeserializeObject<DataFromCameraModel>(reader.ReadToEnd());
                
           }

Thank you. ok.

o-medizadeh-Ard avatar Mar 07 '22 16:03 o-medizadeh-Ard