文件生成和下载angular 7,dotnet核心2

时间:2019-03-04 13:31:44

标签: angular file download .net-core

我有一个angular / dotnet核心应用程序,需要在服务器上生成excel / csv文件,然后下载。

尝试了包括epplus.core在内的大量解决方案,但似乎都没有返回文件(下载文件)。出于某种原因,我总是在网络响应中得到哈希值。

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)
  );
}

服务器:

  [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;
    }
}

网络http响应:

enter image description here

代码仍在构建中,因此请记住任何未使用的变量等。

基本思想是我有一个字符串数组,我将需要将该数组下载到excel / csv文件中。我们正在通过docker使用微服务架构。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

这不是哈希。这是一个字节数组。 因此,如果您要下载此文件,只需将用户重定向到该地址即可。