Exporting file from custom section
m
Umbraco 10. I have set up a custom section and am trying to export an Excel file from a custom table. There is a form with date fields in a dashboard .html file and then the .controller.js file and then GetGatedExport() is in an API Controller. It all works until the very last stage, the content is retrieved from the database and written to the file, I can follow with console.log and Console.WriteLine until the line before: return File(content, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "dataexport.xlsx"); But no file is written. There are no errors either in the page Console or in Command prompt. I originally had it set up slightly differently and it worked then with just an export button - the file was generated and downloaded - but I couldn't get the date fields to work so moved the form to the dashboard and just rewrote that end slightly, but the GetGatedExport() is the same, so I really don't know why it's not working.
Copy code
html
<div class="welcome-dashboard" ng-controller="GatedDataExportController as vm">
    ...
                <div class="form-group">
                    <label for="startDate">Start Date:</label>
                    <input type="date" id="startDate" ng-model="startDate">
                </div>
                <div class="form-group">
                    <label for="endDate">End Date:</label>
                    <input type="date" id="endDate" ng-model="endDate">
                </div>
                <button class="btn btn-primary" ng-click="exportData()">Export</button>
            ..
</div>
Copy code
js
angular.module("umbraco").controller("GatedDataExportController", function ($scope, $http) {
    var vm = this;
    alert("hello world");
    $scope.exportData = function () {
        var startDate = $scope.startDate.toISOString();
        var endDate = $scope.endDate.toISOString();
        // Call backend API to trigger export
        $http.get("/umbraco/backoffice/Website/DataExport/GetGatedExport?from=" + startDate + "&to=" + endDate)
            .then(function (response) {
                console.log("Export successful");
            })
            .catch(function (error) {
                console.error("Export failed:", error);
            });
    };
});
Copy code
cs
public IActionResult GetGatedExport(string from, string to)
    {
        // get dates ...

        using (var scope = scopeProvider.CreateScope(autoComplete: true))
        {
            var db = scope.Database;
            string strSProc = String.Format(";EXEC sp_gac_export @@gac_startDate='{0}', @@gac_endDate='{1}'", fromDate, toDate);

            var results = db.FetchMultiple<ExportRegistrations, ExportDownloads, ExportDownloadByRegistration, ExportDownloadByEmail>(strSProc);

            List<ExportRegistrations> exportRegistrations = results.Item1;
            //....

            if (exportRegistrations.Count > 0 || exportDownloads.Count > 0 || exportDownloadByRegistrations.Count > 0 || exportDownloadByEmails.Count > 0)
            {
                var workbook = new XLWorkbook();

                // Export Registrations
                if (exportRegistrations.Count > 0)
                {
                    var worksheet = workbook.Worksheets.Add("ExportRegistrations");
                    PopulateWorksheet(worksheet, exportRegistrations);
                }

                //...continued

                // Create a memory stream to save the workbook
                using (var stream = new MemoryStream())
                {
                     Console.WriteLine("using var stream");
         workbook.SaveAs(stream);
         var content = stream.ToArray();
             Console.WriteLine("return file");
                    return File(content, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "dataexport.xlsx");
                }
            }
            else
            {
                // If there are no records to export, return an empty file
                return File(new byte[0], "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "empty.xlsx");
            }
        }
    }
If I go into devtools > Network and double click on the relevant URL - http://localhost:7171/umbraco/backoffice/Website/DataExport/GetGatedExport?from=2024-03-01T00:00:00.000Z&to=2024-03-28T00:00:00.000Z - the file generates and downloads
Turns out the file was being generated but not downloaded to the browser, all sorted now.
37 Views