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"
));
}
}
}
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.
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)
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 :
Hi Kevin,
I'm trying to follow your answer and links to examples to be able to export a csv file of members.
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.
Any pointers as to why that might be? I'm at a loss.
Cheers
Rob
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
is working on a reply...