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?
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();
}
}
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.
This only returns Json. I guess I could use a plain old SurfaceController but wouldn't this compromise security?
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/
This may be old, but this is how I solved it:
I created an export button with angular with exportForms as the action
I then built an authorization handler. Just something simple to check if the user was logged in
Then I built a surface controller which throws an unauthorized if someone is not logged in.
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.
Hope that helps
like
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!
is working on a reply...
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.