Search code examples
ajaxasp.net-mvc-5controllermultiple-file-upload

ajax returns error 404/can't find controller when uploading multiple files ASP.net


It seems like my ajax can't find the controller when uploading multiple big sized files

My view:

<input type="file" id="examen_ingresar" name="examen_ingresar" multiple>

My Js:

$.ajax({
    type: "POST",
    url: '/Analisis/IngresarExamen?id_analisis=' + id_analisis ,
    contentType: false,
    processData: false,
    data: data,
    success: function (result) {
        retorno = result;
    },
    error: function (xhr, status, p3, p4) {
        var err = "Error " + " " + status + " " + p3 + " " + p4;
        if (xhr.responseText && xhr.responseText[0] == "{")
            err = JSON.parse(xhr.responseText).Message;
        console.log(err);                    
        }
});

AnalisisController.cs:

[HttpPost]
public JsonResult IngresarExamen(long id_analisis)
{
    int retorno = 0;
    try
    {
        for (int i = 0; i < Request.Files.Count; i++)
        {
            HttpPostedFileBase file = Request.Files[i];
            string direccion = Path.Combine(Server.MapPath("/AppUploads/" + id_analisis), Guid.NewGuid() + Path.GetExtension(file.FileName));
            System.IO.Directory.CreateDirectory(Server.MapPath("/AppUploads/"+ id_analisis));
            retorno += amodel.IngresarExamen(id_analisis, direccion);//Just a method that make the register, if it's ok returns 0
            System.IO.Stream fileContent = file.InputStream;
            file.SaveAs(Path.Combine(direccion));
        }
    }
    catch (Exception)
    {
        retorno++;
        Response.StatusCode = (int)HttpStatusCode.BadRequest;
    }
    return Json(retorno);
}

I also added these lines to the Web.config because maybe it could be by file size (allowing to upload 5gb)

<system.web>
    <httpRuntime targetFramework="4.6.1" maxRequestLength="5242880" executionTimeout="2400" />
    <compilation debug="true" targetFramework="4.6.1"/>
</system.web>

<system.webServer>
    <security>
        <requestFiltering>
            <requestLimits maxAllowedContentLength="5242880"/>
        </requestFiltering>
    </security>
</system.webServer>

This actually is working for some little files so i test it with 3 files that makes 1gb, after like 5 seconds it shows in the browser console:

POST http://localhost:60139/Analisis/IngresarExamen?id_analisis=40 404 (Not Found)

Solution

  • well after searching for this i recognize my mistake and is actually on the Web.config

    <system.web>
        <httpRuntime targetFramework="4.6.1" maxRequestLength="5242880" executionTimeout="2400" />
        <compilation debug="true" targetFramework="4.6.1"/>
    </system.web>
    
    <system.webServer>
        <security>
            <requestFiltering>
                <requestLimits maxAllowedContentLength="5242880"/>
            </requestFiltering>
        </security>
    </system.webServer>
    
    ->maxRequestLength is in kb
    ->executionTimeout is in seconds
    ->maxAllowedContentLength is in BYTES
    

    so if you are getting this same problem try to change these values to the ones you need ALSO maxAllowedContentLength max value it's like 4gb unless you use another method to upload your files