Luis Valencia
Luis Valencia

Reputation: 33988

404 after posting a big file with .net

I have the following JS code, this function calls a WEBAPI service which receives a file and saves it to the server.

Please check the .onfail,

File size is 500MB

function GuardarOrigen() {
        var NombreOrigen = $("#Nombre").val()
        if (NombreOrigen == '') {
            info('Debe ingresar un nombre');
            return;
        }
        var rgx = new RegExp("^[a-zA-Z0-9]+$");

        if (!rgx.test(NombreOrigen)) {
            info('El nombre no debe tener espacios en blanco ni caracteres especiales.');
            return;
        }
        var files = $('#Archivo').get(0).files;

        if (files.length > 0) {
            if (window.FormData !== undefined) {
                var data = new FormData();
                for (var x = 0; x < files.length; x++) {
                    data.append("file" + x, files[x]);
                }



                $("#bntGuardar").prop('disabled', true);
                $("#imgLoader").show();
                $.ajax({
                    url: urlCrearOrigen + '?NombreOrigen=' + NombreOrigen,
                    method: "POST",
                    contentType: false,
                    processData: false,
                    data: data,
                    success: function (result) {
                        info("Guardado con éxito.");
                        window.location.href = urlEditarOrigen + result
                    },
                }).fail(function (jqXHR, textStatus) {
                    info("Error guardando el origen");

                    console.log('Error:' + jqXHR.responseText + textStatus);

                }).always(function () {
                    $("#imgLoader").hide();
                    $("#bntGuardar").prop('disabled', false);


                });
            }
        } else {
            info('Debe seleccionar el archivo');
        }

    }

On the WEB API Controller I have this code:

[HttpPost]
        [Route("/CrearOrigen")]
        public async System.Threading.Tasks.Task<HttpResponseMessage> CrearOrigen(string NombreOrigen)
        {

            if (!Request.Content.IsMimeMultipartContent())
            {
                return this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "No se recibieron archivos");

            }

            string root = HttpContext.Current.Server.MapPath("~/App_Data/CargaOrigenes");
            if (!Directory.Exists(root))
            {
                Directory.CreateDirectory(root);
            }
            var provider = new MultipartFormDataStreamProvider(root);
            await Request.Content.ReadAsMultipartAsync(provider);
            if (provider.FileData.Count == 0)
            {
                return this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Parámetros invalidos - sin archivo");
            }

            string FileName = provider.FileData[0].LocalFileName;
            string[] FileParts = provider.FileData[0].LocalFileName.Split(Convert.ToChar("."));
            string FileExt = String.Empty;
            if (provider.FileData[0].Headers.ContentDisposition.FileName.Contains(".csv"))
            {
                FileExt = "csv";
            }
            else
            {
                return this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Archivo no soportado");
            }

            try
            {

                CargasMasivasManager cmm = new CargasMasivasManager();
                int idOrigen = cmm.CrearOrigen(FileName, FileExt, NombreOrigen);

                return this.Request.CreateResponse(HttpStatusCode.OK, idOrigen.ToString());
            }
            catch (Exception e)
            {
                return this.Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e.Message);
            }

        }

After 10 or 15 minutes I get a 404, the resource you are looking for is not on the server or is unavailable.

However when the files are smaller the process works fine.

Also, this works locally but when deployed on one server I get the error, and I cant debug on the server because I cant install Visual studio there.

Upvotes: 0

Views: 70

Answers (1)

Canvas
Canvas

Reputation: 5897

Try updating your web.config to allow a large file

<configuration>
  <system.web>
    <httpRuntime maxRequestLength="xyz" />
  </system.web>
</configuration>

xyz are in KB, so 2048kb = 2mb. So 500mb = 512000kb

Upvotes: 1

Related Questions