0

I have an angular / dotnet core application where i need to generate a excel / csv file on server and then download it.

Tried tons of solutions including epplus.core but none seem to return the file (download it). For some reason i always get a hashed value in my network response.

UI:

employeesDownload(account: string, sid: string): Observable<any> {
let params = new HttpParams();
params = params.set("sid", sid);
let headers = new HttpHeaders();

return this.http.get(`em/api/v1/employees/${account}/download`, { headers:headers, params: params })
  .pipe(
    map((response: any) => response)
  );
}

Server:

  [HttpGet("{account}/download")]
    public async Task<IActionResult> EmployeesDownloadResult(string sid)
    {
        int _sid = Convert.ToInt32(sid);

        if (_sid > 0)
        {
            //procedure call by sid
            StoredProcedureResponse legacyResponse = await this._legacyUdtApiClient.ApiV1EmployeeGetImportedResultAsync(_sid);

            if (legacyResponse.ReturnValue.Equals("OK", StringComparison.OrdinalIgnoreCase))
            {
                DataSet result = JsonConvert.DeserializeObject<DataSet>(legacyResponse.OutputParameter.ToString());
                // DataSet To CSV conversion needs to be tested
                string CSV = ConversionHelper.ConvertWorkersDataSetToCSV(result);
                string[] strings = CSV.Split(',');
                byte[] file1;

                byte[] bytes;
                MemoryStream stream = new MemoryStream();
                using (ExcelPackage package = new ExcelPackage(stream))
                {
                    ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("Employee");
                    //First add the headers
                    worksheet.Cells[1, 1].Value = "ID";
                    worksheet.Cells[1, 2].Value = "Name";
                    worksheet.Cells[1, 3].Value = "Gender";
                    worksheet.Cells[1, 4].Value = "Salary (in $)";

                    //Add values
                    worksheet.Cells["A2"].Value = 1000;
                    worksheet.Cells["B2"].Value = "Jon";
                    worksheet.Cells["C2"].Value = "M";
                    worksheet.Cells["D2"].Value = 5000;

                    worksheet.Cells["A3"].Value = 1001;
                    worksheet.Cells["B3"].Value = "Graham";
                    worksheet.Cells["C3"].Value = "M";
                    worksheet.Cells["D3"].Value = 10000;

                    worksheet.Cells["A4"].Value = 1002;
                    worksheet.Cells["B4"].Value = "Jenny";
                    worksheet.Cells["C4"].Value = "F";
                    worksheet.Cells["D4"].Value = 5000;
                    bytes = package.GetAsByteArray();
                }

                HttpContext.Response.Headers.Add("Content-Disposition", "inline; filename=[filename]");
                return File(bytes, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "test.xlsx");
            }
        }
        else
            throw new Exception($"download failed");

        return null;
    }
}

Network http response:

enter image description here

Code is still under construction so bare in mind any unused variables etc.

The basic idea is that I have a array of strings, I will need to download that array in a excel/csv file. We are using micro service arch via docker. Any help would be highly appreciated.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Edin
  • 32
  • 8
  • First of all, if you just use the url of the download in your browser, do you get the file ? I mean "em/api/v1/employees/${account}/download" ? – JFPicard Mar 04 '19 at 13:35
  • that is the URL to my REST API controller action that should generate the file in runtime and send it to the client – Edin Mar 04 '19 at 13:38
  • 1
    Try [this](https://stackoverflow.com/a/52588799/5729813) for the Angular part – Tushar Walzade Mar 04 '19 at 13:42
  • @TusharWalzade Worked like a charm, thanks a lot! – Edin Mar 04 '19 at 14:40
  • Your server is returning what looks very much like a valid response. That "text" in the response starting "PK..." is a zip file, which is exactly what an Excel (.XLSX) file should look like. But, you need some code on the client side to download that file and/or open it in the browser. Simply making the http.get call will not achieve that. – Gary McGill Mar 04 '19 at 14:42

1 Answers1

0

This is not hash. This is a byte array. So if you want to download this file, simply redirect the user to this address.

Danny
  • 53
  • 2
  • 4