Home > Software design >  .net Core send xlsx file as response for Ajax request
.net Core send xlsx file as response for Ajax request

Time:06-16

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?

CodePudding user response:

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");
        }
  • Related