Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

307
Vistas
.net Core send xlsx file as response for Ajax request

My web-application can only work with .xlsx-files. I created a function in my controller, which converts a .xls-file into an .xlsx-file. The conversion works totally fine. So whenever I try to open a .xls-file, I send it via Ajax request.

After that, I want to respond with the converted .xlsx-file, but it never arrives at the client.

Since the conversion works fine, i simplified the code, to just pass a simple .xlsx file, which didn't work aswell.

JavaScript:

if (files[0].type == "application/vnd.ms-excel") {                
    console.log("XLS-File");

    formData = new FormData();
    formData.append("xlsfile", files[0]);

    $.ajax({
        url: '/Home/ConvertToXlsx',
        type: 'POST',
        cache: false,
        processData: false,
        data: formData,
        contentType: false,
        beforeSend: function (xhr) {
            xhr.setRequestHeader("XSRF-TOKEN",
            $('input:hidden[name="__RequestVerificationToken"]').val());
        },
        success: function (response) {                        
            openXLSFile(response);            
        },
        error: function () {
            console.log("An Error occurred");
        }
    });
}

Controller:

public IActionResult ConvertToXlsx(IFormFile xlsfile) {

    //For simplification without conversion
    //var xlsconverter = new XLSConverter();
    //FileStreamResult response = xlsconverter.XLSConvert(xlsfile);

    var path = $"wwwroot/temp/";
    var filepath = Path.Combine(path, "test.xlsx");
    MemoryStream x = new MemoryStream();
    using(FileStream fs = System.IO.File.OpenRead(filepath)) {
        fs.CopyTo(x);
    }

    return File(x, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");

}

In the browser-network tab I get Status: net::ERR_CONNECTION_RESET screenshot here

Am I missing a setting somewhere, which causes I simply can't respond with files?

about 4 years ago · Santiago Gelvez
1 Respuestas
Responde la pregunta

0

If you want to pass file to action,try to use the following code:

if (files[0].type == "application/vnd.ms-excel") {                
    console.log("XLS-File");

    $.ajax({
        url: '/Home/ConvertToXlsx',
        type: 'POST',
        cache: false,
        processData: false,
        data: files[0],
        contentType: false,
        beforeSend: function (xhr) {
            xhr.setRequestHeader("XSRF-TOKEN",
            $('input:hidden[name="__RequestVerificationToken"]').val());
        },
        success: function (response) {                        
            openXLSFile(response);            
        },
        error: function () {
            console.log("An Error occurred");
        }
    });
}

And if you want to return file with content type,you can try to use:

public IActionResult ConvertToXlsx(IFormFile xlsfile)
        {
            return File(xlsfile.OpenReadStream(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        }
about 4 years ago · Santiago Gelvez Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda