upload a file with ajax in mvc

1

Very good everyone, I wanted to know if they could help me with a problem, I'm trying to upload a file from Ajax to send to a controller. The error I have is that it does not let me load large files in Ajax, it sends me to the Ajax error. The problem occurs by doing it submit type or before pressing the button to send the controller, the code is the following from Ajax:

function ActualizarArrayAdjuntos(_archivo) {
    $.ajax({
        cache: false,
        async: false,
        url: "@Url.Action("ActualizarArrayAdjuntos")",
        type: "POST",
        data: JSON.stringify({ archivo: _archivo, tipoMantenimiento: _tipoMantenimiento }),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        error: function (response) {
            MsgInformacion(EtiquetasMensaje.Fallo, EtiquetasMensaje.OperacionError);
        },
        success: function (response) {
            if (response == -1) {
                MsgInformacion(EtiquetasMensaje.Fallo, "Ocurrio un error a la hora de adjuntar el archivo.");
            }
        }
    });
}

When I try to attach the file it shows the message MsgInformation (MessageMessage.Failure, MessageMessage.OperationError), of the error, this only happens with large files.

Thank you very much

    
asked by Mauricio Acon 03.07.2017 в 17:54
source

2 answers

1

You need to increase the maximum size allowed in the server request, in the same way, the execution timeout using the <httpRuntime> section in your web.config:

<system.web>
    <httpRuntime 
        maxRequestLength="tamaño en kbytes"
        executionTimeout="segundos"
    />
...        
</system.web>

And in case you are doing a deploy in IIS 7.0+ you may need to increase the maximum size allowed in the request by using <requestLimits> within the <system.webServer> section:

<system.webServer>
    <security>
        <requestFiltering>
            <requestLimits maxAllowedContentLength="tamaño en bytes" />
        </requestFiltering>
    </security>

    ...
</system.webServer>

Original reply in SOen: link

    
answered by 03.07.2017 в 18:24
0

increase the size in your app.config

<system.web>
    <httpRuntime executionTimeout="500" maxRequestLength="29296" />   </system.web>
    
answered by 09.08.2018 в 17:34