Copied to clipboard

Flag this post as spam?

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


  • Matthieu Nelmes 102 posts 385 karma points
    Nov 11, 2015 @ 17:26
    Matthieu Nelmes
    0

    Exporting CSV from BackOffice (non usercontrol)

    The decision to try a non usercontrol approach was because I've managed to create a custom property editor that renders a list-view. So I wanted to stick with my angular/MVC approach.

    Struggling to see how I can force download as it seems the UmbracoAuthorizedApiController only returns Json so I'm open to suggestions.

    [Umbraco.Web.Mvc.PluginController("My.Admin")]
    public class RoomListExportApiController : UmbracoAuthorizedApiController
    {
        [System.Web.Http.AcceptVerbs("GET", "POST")]
        [System.Web.Http.HttpGet]
        public FileContentResult DownloadCsv()
        {
            string csv = "test,test1,test2";
            FileContentResult result = new FileContentResult(new System.Text.UTF8Encoding().GetBytes(csv), "text/csv");
            return result;
        }
        }
    

    This only returns Json. I guess I could use a plain old SurfaceController but wouldn't this compromise security?

  • Matthieu Nelmes 102 posts 385 karma points
    Nov 11, 2015 @ 18:39
    Matthieu Nelmes
    0

    Found a solution! A well written tutorial here was exactly what I was looking for!

    http://umbracodevelopers.sdesign1dev.co.uk/2015/7/30/ajax-newsletter-sign-up/

  • Taylor Mitchell 19 posts 42 karma points
    Nov 16, 2015 @ 22:16
    Taylor Mitchell
    1

    This may be old, but this is how I solved it:

    I created an export button with angular with exportForms as the action

    exportForms: function(type) {
                return $http.post("backoffice/MySection/ExportAction/export");
            }
    

    I then built an authorization handler. Just something simple to check if the user was logged in

    public class AuthorizationHandler
    {
        public static bool UserLoggedIn()
        {
            var httpCtxWrapper = new HttpContextWrapper(HttpContext.Current);
            var umbTicket = httpCtxWrapper.GetUmbracoAuthTicket();
            if (umbTicket == null || umbTicket.Name.IsEmpty() || umbTicket.Expired)
                return false;
            return true;
        }
    }
    

    Then I built a surface controller which throws an unauthorized if someone is not logged in.

    public class ExportActionsController : SurfaceController
    {
        public ActionResult Export(string type)
        {
            if (AuthorizationHandler.UserLoggedIn())
            {
                var query = //my sql statement here
                var forms = DatabaseContext.Database.Fetch<dynamic>(query);
    
                ExcelService.ExportForm(forms, type);
    
                if (HttpContext.Request.UrlReferrer != null)
                {
                    return Redirect(HttpContext.Request.UrlReferrer.AbsoluteUri);
                }
                return Redirect("/#/MySection");
            }
            else
            {
                throw new HttpException(401, "Unauthorized access");
            }
        }
    }
    

    Then I had to handle the exporting. I like to use closedXML.

    I'm modifying this code a bit because my original is a bit more complex.

        public class ExcelService
       {
           public static void ExportForm<T>(IEnumerable<T> forms, string filename)
          {
            XLWorkbook wb = new XLWorkbook();
    
            //this handles exporting my data into a datatable. You have to build this yourself.
            DataTable dt = ToDataTable(forms.ToList());
    
            //adds a worksheet with the data and the filename is the sheet name
            wb.Worksheets.Add(dt, filename);
    
            //This handles the download.
            HttpResponse httpResponse = HttpContext.Current.Response;
            httpResponse.Clear();
    
            //tell it the content type
            httpResponse.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    
            //give it your filename
            httpResponse.AddHeader("content-disposition", "attachment;filename=\"" + filename + " " + DateTime.Now.ToShortDateString() + ".xlsx\"");
    
            MemoryStream memoryStream = new MemoryStream();
    
            //save the workbook as a memory stream
            wb.SaveAs(memoryStream);
    
            //write file to users computer
            memoryStream.WriteTo(httpResponse.OutputStream);
    
            //never forget to close the stream and the response
            memoryStream.Close();
    
            httpResponse.End();
           }
        }
    

    Hope that helps

  • Biagio Paruolo 1494 posts 1635 karma points c-trib
    Dec 02, 2015 @ 15:04
    Biagio Paruolo
    0

    like

  • Matthieu Nelmes 102 posts 385 karma points
    Nov 16, 2015 @ 23:22
    Matthieu Nelmes
    0

    Thanks Taylor, I'll take a look.

    I did get a version working with some AngularJs magic but it's always worth exploring other options!

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies