Request error to the server when clicking on an ASP.Net C # image

0

I show some images on a page that when I clicked it returns the error:

  

"Sys.WebForms.PageRequestManagerServerErrorException: An unknown error occurred while processing the request on the server. The status code returned from the server was: 500".

To show the images I use a DataList with an ImageButton.
Checking the error code in the global.asax I found that the error is due to "Se excedió la longitud de solicitud máxima."

Regarding this topic I found many posts (which do not describe this problem because I see that they have it when uploading the images, I have it when trying to work with the images) indicating that the maxRequestLength should be modified on the web. config but to be able to save large images since ASP.Net accepts files up to 4MB by default. This I did when I saved the images and it worked perfectly, I keep the images and I show them on the page without problems.

I leave the web.config

    <location path="ClientesValidarImagenes.aspx">
<system.web>
  <httpRuntime requestValidationMode="2.0" enable="false" maxRequestLength="1048576 "  requestLengthDiskThreshold="102400 " useFullyQualifiedRedirectUrl="true" executionTimeout="3600" targetFramework="4.5"/>
</system.web>
<system.webServer>
  <security>
    <requestFiltering>
      <requestLimits maxAllowedContentLength="1073741824" />
    </requestFiltering>
  </security>
</system.webServer>

Now when I want to select an image to add a class (and then work with the IDs of the selected images) it throws me the error that I described previously and does not get to execute the OnItemCommand which is where I implement the code to add the class.

Code aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="ClientesValidarImagenes.aspx.cs" Inherits="GD.ModuloParametros.ClientesValidarImagenes" EnableEventValidation="false"%>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
        <asp:Accordion ID="accValidarImagenes" runat="server" FadeTransitions="True" FramesPerSecond="40" Width="100%"
                    TransitionDuration="200" RequireOpenedPane="false" SuppressHeaderPostbacks="true" HeaderCssClass="accordionHeader"
                     ContentCssClass="accordionContent" HeaderSelectedCssClass="accordionHeader_selected">
            <Panes>
                <asp:AccordionPane ID="accPaneValidarImagenes" runat="server">
                    <Header>
                        <div class="accordionTitulo">
                            <span class="TituloFiltros" id="TituloFiltros">
                                <asp:Literal ID="Literal1" runat="server" Text="<%$ Resources:GiroDinero, TituloValidarImagenesClientes%>"></asp:Literal>
                            </span> - Seleccione las imágenes que desea validar. 
                        </div>
                    </Header>
                    <Content>
                        <asp:UpdatePanel runat="server">
                            <ContentTemplate>
                                <asp:DataList ID="dlImagenes" ClientIDMode="Static" runat="server" RepeatColumns="3" RepeatDirection="Horizontal" HorizontalAlign="Center" PagedControlID="dlImagenes" CellSpacing="10" OnItemCommand="dlImagenes_ItemCommand" > 
                                    <ItemTemplate>
                                        <asp:ImageButton ID="imgValidarImagenes" runat="server" Width="200px" Height="150px" ImageUrl='<%# Bind("imagen") %>' OnClientClick="modImg(this)" CommandArgument='<%# Container.ItemIndex %>' CssClass="previewImg" ViewStateMode="Enabled" />
                                    </ItemTemplate>                                                                                                    
                                </asp:DataList>
                            </ContentTemplate>
                            <Triggers>
                                <asp:AsyncPostBackTrigger ControlID="dlImagenes"/>
                            </Triggers>
                        </asp:UpdatePanel>
                        <div style="text-align:end; padding:10px;" id="divBtnValidar">
                             <asp:Button ID="btnValidar" runat="server" Text="Validar" CssClass="lnkDocumento" />
                        </div>
                    </Content>
                </asp:AccordionPane>
            </Panes>
        </asp:Accordion><br />
</asp:Content>  

I leave the aspx.cs but I do not know if it is really necessary since at this point the code does not get executed.

protected void dlImagenes_ItemCommand(object source, DataListCommandEventArgs e)
    {
        dlImagenes.SelectedIndex = e.Item.ItemIndex;
        ImageButton img = (ImageButton)dlImagenes.SelectedItem.FindControl("imgValidarImagenes");

        if (!img.CssClass.Contains("imagenSeleccionada"))
        {
            //agrego la clase
            img.CssClass = String.Join(" ", img.CssClass.Split(' ').Except(new string[] { " ", "imagenSeleccionada" }).Concat(new string[] { "imagenSeleccionada" }).ToArray());
        }
        else
        {
            //Remuevo la clase
            img.CssClass = String.Join(" ", img.CssClass.Split(' ').Except(new string[] { " ", "imagenSeleccionada" }).ToArray()); 
        }

    }  

Investigating a bit (enough) I saw that the request size of the page should not exceed 4MB - correct me if it is not - that is the maxRequestLength that I configured in the web.config (in many posts I saw that makes reference that the 4MB corresponds to the total size of the files to upload but seeing this I understand that it is the total weight of the page). I searched for the size of the request and returned 4.544.062 bytes (represented in an int) that would be 4.33MB so the error occurs. I show in an image the size of the content of the page.

One more clarification, the images that I save do not even weigh 1MB, but when the request is made and it is less than 4,000,000 bytes everything works without prolemas.

The configuration of the web.config for what I see is only so that I can upload images larger than the 4MB allowed by default, but how do I work with the loaded images if the request can not exceed 4MB?

    
asked by Pablo Matias 04.07.2018 в 16:38
source

0 answers