1

I'm trying to retrieve images from my database, works fine with little images but when I try it with the Windows 7 default images (Desert, Koala, Penguins, Tulips, etc.) doesn't work and I get this error:

"Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property."

I modify my web.config with this:

<scripting>
  <webServices>
    <jsonSerialization maxJsonLength="2147483647"/>
  </webServices>
</scripting>

What I use to retrieve the images is this code:

public static byte[] Serialize(object obj)
{
    var binaryFormatter = new BinaryFormatter();
    var ms = new MemoryStream();
    binaryFormatter.Serialize(ms, obj);
    return ms.ToArray();
}


[WebMethod]
public string ObtenerImagen(int id)
{
    DataTable dt = new DataTable();        
    dt = conn.consultarImagen("alertas", id);
    Imagenes img;
    List<Imagenes> lista = new List<Imagenes>();

    for (int i = 0; i < dt.Rows.Count; i++)
    {
        img = new Imagenes();
        img.Id = Convert.ToInt32(dt.Rows[i]["Id"]);
        img.FileName = dt.Rows[i]["FileName"].ToString();
        img.Type = dt.Rows[i]["ContentType"].ToString();
        img.Content = Convert.ToBase64String(Serialize(dt.Rows[i]["Content"]));
        img.IdAlerta = Convert.ToInt32(dt.Rows[i]["IdAlerta"]);
        img.Pie = dt.Rows[i]["PieFoto"].ToString();
        //Llenado de lista
        lista.Add(img);
        img = null;
    }
    JavaScriptSerializer js = new JavaScriptSerializer();
    string lineas = js.Serialize(lista);
    return lineas;
}

And then I use this javascript function with ajax:

 success: function (data) {
        var aRC = JSON.parse(data.d);
        var lineas = "";

        for (var i = 0; i < aRC.length; i++) {
            var id = aRC[i].Id;
            var num = id;
            var rev = aRC[i].FileName;
            var pur = aRC[i].Type;
            var status = aRC[i].Content;
            var imagen = status.substring(36, status.length - 37);
            var owner = aRC[i].IdAlerta;
            var pie = aRC[i].Pie;

            lineas += '<div class="col-lg-3 col-md-4 col-xs-3 thumb marco">';
            lineas += '<a class="thumbnail" href="#">';
            lineas += '<img class="responsive" src="data:image/jpeg;base64,' + imagen + '" />';                    
            lineas += '<p class="text-justify" id="Pie' + i + '">' + pie + '</p>';
            lineas += '</a>';
            lineas += '<span class="btn btn-xs btn-success fa fa-pencil hidden-print" id="EditPie' + i + '"></span>';
            lineas += '<input type="text" class="form-control hidden hidden-print" id="PiePag' + i + '"> <span class="btn btn-xs btn-success fa fa-check hidden hidden-print" id="OkPie' + i + '"></span>';
            lineas += '</div>';
      }
      $('#Imagenes').html(lineas);

What can I do tho solve this? I don't have any idea.

EDIT: Works fine with 1 image, problem occurs when I try to show more than 1 image

5
  • Is this MVC by any chance? Commented Aug 4, 2016 at 15:26
  • No, is not MVC, it just HTML and C# WebService with AJAX calls and JavaScript. Commented Aug 4, 2016 at 15:27
  • 1
    Ah I see, apologies. It's a known caveat in MVC that the Web.Config maxJsonLength is essentially ignored and must be declared within code. I'll leave someone else to answer your question, as I don't have any other suggestion. Good luck! Commented Aug 4, 2016 at 15:29
  • Please edit your tags. If that's not mvc Is that asp.net? Commented Aug 4, 2016 at 15:35
  • Yes, its asp.net, what tag should I edit? Commented Aug 4, 2016 at 15:37

1 Answer 1

1

Try setting the max JSON length via the instance you created like this:

js.MaxJsonLength = Int32.MaxValue;

See if that makes a difference, how big are your objects that you are trying to shove into it? JSON doesn't perform as well with a list of large objects, might be easier to lazy load them via AJAX. If you look at large galleries, they tend to show a loading image while its in the background retrieving the specific object for the next items and so forth.

Sign up to request clarification or add additional context in comments.

3 Comments

I edit this code: var js = new JavaScriptSerializer { MaxJsonLength = Int32.MaxValue }; string lineas = js.Serialize(lista); Apparently works fine at least I tried it with 4 images, thanks bro.
That is great, do you have an avg. on the image sizes at all? I was under the impression that a service or MVC would ignore it from the web config file. I would check out lazy loading everything via ajax though seriously, it will give your users a better viewing exp.
Less than 2mb. Now I tried with more than 10 images and works fine! so the problem was that, the service was ignoring the web.config

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.