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
    0

    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";
                #endregion
    
                #region Body
                foreach (var member in members)
                {
                    currentRow++;
                    worksheet.Cell(currentRow, 1).Value = member.Id;
                    worksheet.Cell(currentRow, 2).Value = member.Name;
                    worksheet.Cell(currentRow, 3).Value = member.Email;
                }
                #endregion
    
                using (var stream = new MemoryStream())
                {
                    workbook.SaveAs(stream);
                    var content = stream.ToArray();
                    return new ApiResponse( true,"done",File(
                        content,
                        "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
                        "AllMembers.xlsx"
                        ));
    
                }
    
            }
        }
    
  • Kevin Jump 2343 posts 14890 karma points MVP 8x c-trib
    Jun 29, 2022 @ 10:04
    Kevin Jump
    1

    Hi,

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

    https://github.com/KevinJump/DoStuffWithUmbraco/tree/v8/Src/DoStuff.Core/FileDownload

    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: https://github.com/KevinJump/DoStuffWithUmbraco/blob/v8/Src/DoStuff.Core/App_Plugins/DoStuff.Dashboard/defaultDashboardController.js#L60-L77

    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;
    
        umbRequestHelper.downloadFile(url)
            .then(function (result) {
                vm.buttonState = 'success';
                notificationsService.success('Downloaded', 'File downloaded');
            }, function (error) {
                vm.buttonState = 'error';
                notificationsService.error('Error', 'Failed to download');
            });
    }
    
  • Robert Graham 8 posts 102 karma points
    Nov 29, 2022 @ 22:13
    Robert Graham
    0

    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) {
            notificationsService.success(value);
        })
    }, function (error) {
        localizationService.localize("speechBubbles_memberExportedError").then(function (value) {
            notificationsService.error(value);
        })
    });
    

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

    Cheers

    Rob

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

    Hi Rob,

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

    thanks

    Ed

Please Sign in or register to post replies

Write your reply to:

Draft