Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at

  • Ziad ba 2 posts 72 karma points
    Jun 29, 2022 @ 08:43
    Ziad ba

    Download Excel files from a Backoffice section

    Hello I still don't have that good experience with Umbraco, but I have an issue with finding the proper code to allow admins to download an excel file that was generated in the C# API controller, tried many different ways using jQuery and JavaScript, and none of them worked, My backend code that was used to get all members just as an example: (used ClosedXML.Excel package)

            public ApiResponse DownloadAllMembersFile()
                var members = Services.MemberService.GetAllMembers();
            using (var workbook = new XLWorkbook())
                var worksheet = workbook.Worksheets.Add("AllMembers");
                var currentRow = 1;
                #region Header
                worksheet.Cell(currentRow, 1).Value = "StudentId";
                worksheet.Cell(currentRow, 2).Value = "Name";
                worksheet.Cell(currentRow, 3).Value = "Email";
                #region Body
                foreach (var member in members)
                    worksheet.Cell(currentRow, 1).Value = member.Id;
                    worksheet.Cell(currentRow, 2).Value = member.Name;
                    worksheet.Cell(currentRow, 3).Value = member.Email;
                using (var stream = new MemoryStream())
                    var content = stream.ToArray();
                    return new ApiResponse( true,"done",File(
  • Kevin Jump 2317 posts 14746 karma points MVP 7x c-trib
    Jun 29, 2022 @ 10:04
    Kevin Jump


    there is an example of how to download a file in the backoffice here :

    I think if your controller is working the missing bit is the umbRequest Helper in the angularJs bit of the back office :

    again example here:

    you will probably end up with something like this :

    function download() {
        var message = 'Hello from here';
        var count = 100;
        vm.buttonState = 'busy';
        var url = Umbraco.Sys.ServerVariables.doStuffFileDownload.downloadService + 'DownloadText' + "?message=" + message + "&count=" + count;
            .then(function (result) {
                vm.buttonState = 'success';
                notificationsService.success('Downloaded', 'File downloaded');
            }, function (error) {
                vm.buttonState = 'error';
                notificationsService.error('Error', 'Failed to download');
  • Robert Graham 7 posts 101 karma points
    Nov 29, 2022 @ 22:13
    Robert Graham

    Hi Kevin,

    I'm trying to follow your answer and links to examples to be able to export a csv file of members.

    var response = new HttpResponseMessage
                Content = new ByteArrayContent(data)
                    Headers =
                        ContentDisposition = new ContentDispositionHeaderValue("attachment")
                            FileName = "members.csv"
                        ContentType = new MediaTypeHeaderValue("text/csv")
            response.Headers.Add("x-filename", "members.csv");
            return response;

    The byte[] I am passing in is created via a nuget package that reads object collections and creates a csv file. (CsvHelper)

    When the file downloads via the back office from my controller.js the file is called download.bin and if I change its name to .txt it seems to only contain the response header information.

    umbRequestHelper.downloadFile("backoffice/Maple/MembershipDashboard/Export").then(function ()
        localizationService.localize("speechBubbles_memberExportedSuccess").then(function (value) {
    }, function (error) {
        localizationService.localize("speechBubbles_memberExportedError").then(function (value) {

    Any pointers as to why that might be? I'm at a loss.



  • Ed Davies 2 posts 72 karma points
    May 07, 2024 @ 13:37
    Ed Davies

    Hi Rob,

    Just curious if you ever found a fix for this, I'm having the same issues downloading a csv in Umbraco 13?



Please Sign in or register to post replies

Write your reply to: