0

asp.net core service return FileContentResult as this format: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
How Can I open on desktop or download on browser the excel file?

Backend

     [HttpPost]
        public IActionResult ExportCashBoxReportToExcell(List<string> cashFlowDates)
        {
        
            using (var package = new ExcelPackage())
            {
                var worksheet = package.Workbook.Worksheets.Add("CashBox");
                var excelData = package.GetAsByteArray();
                var contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                var fileName = "MyWorkbook.xlsx";
                return File(excelData, contentType, fileName);
            }

         
        }

Frontend

  $.ajax({
                async: false,
                type: "POST",
                headers: {"Authorization":  window.localStorage.getItem('token')},
                contentType: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
                data: { cashFlowDates },
                    headers: {
              "accept": "application/json",
              "content-type": "application/json",
              "authorization": "Bearer " + window.localStorage.getItem('token')
        },
                url: '/CashBoxReport/ExportCashBoxReportToExcell',
                success: (response) => {
                    console.log(response)
                },
                error: (error) => {
                    alert("Hata")
                }
            });
        }
heker
  • 45
  • 4
  • Does this answer your question? [Handle file download from ajax post](https://stackoverflow.com/questions/16086162/handle-file-download-from-ajax-post) – Christoph Lütjen Jan 22 '22 at 22:30
  • More options: Change method to GET (then you can simply link to the url) or create a temporary file on the server that can be downloaded with a GET request. – Christoph Lütjen Jan 22 '22 at 22:32
  • Why you want to use ajax to get the excel file? How about directly call the method to get the excel file instead of using the post method. It seems you don't use any cashFlowDates parameters. – Brando Zhang Jan 27 '22 at 08:48

0 Answers0