My goal is to return a .CSV
file which contains Persian text characters from an ASP.NET Core 8.0 Web API controller action method as a FileContentResult
.
I tried the suggested attempts which led me no where.
using Microsoft.AspNetCore.Mvc;
using System.Text;
namespace WebApplication.Controllers
{
[ApiController]
[Route("[controller]")]
public class TestController : ControllerBase
{
[HttpGet("attempt1")]
public IActionResult GetCsv()
{
// Sample data with Persian characters
var csvData = "لورم ایپسوم متن ساختگی با تولید سادگی نامفهوم از صنعت چاپ، و با استفاده از طراحان گرافیک است .";
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
// Get Windows-1256 encoding
var encoding = Encoding.GetEncoding("windows-1256");
// Encode CSV data in Windows-1256
var bytes = encoding.GetBytes(csvData);
// Return the CSV file with proper encoding
return File(bytes, "text/csv", "persian.csv");
}
[HttpGet("attempt2")]
public IActionResult GetCsv2()
{
var csvData = "لورم ایپسوم متن ساختگی با تولید سادگی نامفهوم از صنعت چاپ، و با استفاده از طراحان گرافیک است .";
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
var encoding = Encoding.GetEncoding(1256);
var bytes = encoding.GetBytes(csvData);
return File(bytes, "text/csv", "persian.csv");
}
[HttpGet("attempt3")]
public IActionResult GetCsv3()
{
var csvData = "لورم ایپسوم متن ساختگی با تولید سادگی نامفهوم از صنعت چاپ، و با استفاده از طراحان گرافیک است .";
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
var encoding = Encoding.GetEncoding("windows-1256");
var bytes = encoding.GetBytes(csvData);
// Set the content type and charset in the response headers
Response.Headers.Add("Content-Disposition", "attachment; filename=persian.csv");
Response.ContentType = "text/csv; charset=windows-1256";
return File(bytes, "text/csv", "persian.csv");
}
[HttpGet("attempt4")]
public IActionResult GetCsv4()
{
// Sample data with Persian characters
var csvData = "لورم ایپسوم متن ساختگی با تولید سادگی نامفهوم از صنعت چاپ، و با استفاده از طراحان گرافیک است .";
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
var destEncoding = Encoding.GetEncoding("windows-1256");
var bytes = Encoding.Convert(Encoding.UTF8, destEncoding, Encoding.UTF8.GetBytes(csvData));
return File(bytes, "text/csv", "persian.csv");
}
[HttpGet("attempt5")]
public IActionResult GetCsv5()
{
var csvData = "لورم ایپسوم متن ساختگی با تولید سادگی نامفهوم از صنعت چاپ، و با استفاده از طراحان گرافیک است .";
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
var encoding = Encoding.GetEncoding("windows-1256");
// Encode CSV data in Windows-1256 with BOM (if needed)
var preamble = encoding.GetPreamble(); // Normally, Windows-1256 does not use a BOM, so this will be empty
var bytes = encoding.GetBytes(csvData);
// Combine preamble and byte data (normally preamble is empty for Windows-1256)
var fileContent = new byte[preamble.Length + bytes.Length];
System.Buffer.BlockCopy(preamble, 0, fileContent, 0, preamble.Length);
System.Buffer.BlockCopy(bytes, 0, fileContent, preamble.Length, bytes.Length);
// Return the CSV file with proper encoding
return File(fileContent, "text/csv", "persian.csv");
}
}
}
None of the actions return a .CSV
file with readable text, they provide content which illegible text only:
Also if I open the file as “Notepad++” the encoding is set to UTF-8 and not to “Windows-1256” which is my goal:
Info: I’m using .NET 8.0 on Windows 10 OS.