0
public async Task<IActionResult> DownloadCSVResults([FromBody] ProfilesSearchOptions searchOptions)
    {
         var report = await profileManager.GetRep(searchOptions);
        if (report == null)
        {
            return NotFound();
        }
        var result = Encoding.UTF8.GetPreamble().Concat(report.Body).ToArray();
        return File(result, "text/csv", $"UserProfiles.{DateTime.Now:yyyy.MM.dd.HH.mm.ss}.csv");
    }



public async Task<Report> GetRep(ProfilesSearchOptions searchOptions) 
    {
        if (searchOptions == null)
        {
            return null;
        }

        var searchResult = await SearchProfiles(tenantDomain, false, searchOptions);

        if (searchResult == null)
        {
            return null;
        }
        var report = GenerateReportRecord("Trainee");
        var fileAsBytes = CsvService.GetCSVAsBytesWithHeaders(searchResult.UsersProfiles.Select(m => new UserProfileViewModel
        {
            Id = m.Id,
            FirstNameAr = m.FirstNameAr,
            FatherNameAr = m.FatherNameAr,
            FamilyNameAr = m.FamilyNameAr,
            FullNameAr = m.FullNameAr,
            Email = m.Tenants?.Select(t => t.Email).Aggregate((t1, t2) => t1 + ", " + t2),
            Deleted = m.Deleted.HasValue && m.Deleted.Value ? "Yes" : "No",
        }));

        report.Body = fileAsBytes;
        report.Status = ReportStatus.Success;
        return report;
    }

public static byte[] GetCSVAsBytesWithHeaders<T>(IEnumerable<T> data)
    {
        using (var memory = new MemoryStream())
        using (var writer = new StreamWriter(memory, new UTF8Encoding(true)))
        using (var csvWriter = new CsvWriter(writer))
        {
            csvWriter.Configuration.RegisterClassMap<AutoClassMapWithApplyDisplayNameAttribute<T>>();
            csvWriter.WriteRecords<T>(data);
            writer.Flush();
            var result = memory.ToArray();
            return result;
        }
    }

private Report GenerateReportRecord(string reportTitle, string reportName)
    {
        return new Report
        {
            Id = Guid.NewGuid().ToString(),
            ReportTitle = $"{reportTitle}.{DateTime.Now:yyyy.MM.dd.HH.mm.ss}",
            Status = ReportStatus.InProgress
        };
    }

these are the three main functions that I am using the CSV file is created but with UTF-8 Encoding but as I mentioned, I needed it to be UTF-8-BOM...any help? and thanks in advance... the problem is in my csv file some charater are displaying like that => " الاسم الاول "

someone
  • 139
  • 1
  • 12
  • Related: [Create Text File Without BOM](https://stackoverflow.com/questions/2502990/). Seemingly opposite however could lead to solution… – JosefZ Apr 08 '21 at 15:49
  • he was using `var writer = new StreamWriter(memory, new UTF8Encoding(true)` and the answer was to set the boolean to false the get the file without BOM while in my code it is set to true... – someone Apr 08 '21 at 16:29

0 Answers0