1

I am sending a model to server through webapi ,model contains base 64 encoded image but if i send without encoded string then at server side i am getting all other properties of model ,

but when i include base64 encoded string in model whole model coming as null,

i am using AngularJS at client side and .net WebApi at server

Angular code

var base64ImageString='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMg.......'
var req = {
 method: 'POST',
 url: 'myurl',
 data: { EncodedImage: base64ImageString,Name:'Rajiv' }
}

$http(req).then(function(){...}, function(){...});

Server Side Code

 public HttpResponseMessage Post([FromBody] UserInfo userInfo)
{
  //My Stuff
}

public class UserInfo
{
public String EncodedImage{get;set;}
public String Name{get;set;}
}
Rajiv
  • 1,245
  • 14
  • 28

3 Answers3

1

Is your image too large? Try uploading smaller images (a few 100 KBs in size). If that works, try compressing, or reducing the resolution of the image before encoding it. Here is an example: https://stackoverflow.com/a/20382559/1237117

Another option is to increase the json size limit at your Web API.

<configuration> 
   <system.web.extensions>
       <scripting>
           <webServices>
               <jsonSerialization maxJsonLength="50000000"/>
           </webServices>
       </scripting>
   </system.web.extensions>
</configuration> 
Community
  • 1
  • 1
mridula
  • 3,203
  • 3
  • 32
  • 55
0

Try doing a request first to get an empty object from the backend. Set the properties of the object in javascript, and send the modified object back to the server.

$scope.model = {};

$http.get(apiPath).then(function(result){
    $scope.model = result.data;
});

$scope.model.EncodedImage = base64ImageString;
$scope.model.Name = "Rajiv";

$http.post(apiPath, $scope.model).then(function(result){
    // ...
});

Also remove [FromBody] in the backend, and string in your UserInfo class should be written with a small letter.

brammekuhh
  • 133
  • 1
  • 9
0

You must incresase maxRequestLength under in <httpRuntime ...

I got the answer from forumAsp net

Luis Armando
  • 101
  • 1
  • 4