make all header file responses ascii compliant
This commit is contained in:
parent
9659027b78
commit
b692336c07
|
|
@ -1,3 +1,4 @@
|
||||||
|
using System.Globalization;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Xml;
|
using System.Xml;
|
||||||
using System.Xml.Serialization;
|
using System.Xml.Serialization;
|
||||||
|
|
@ -9,11 +10,44 @@ namespace NejCommon.Controllers;
|
||||||
|
|
||||||
public static class Responses
|
public static class Responses
|
||||||
{
|
{
|
||||||
|
public static string ToAscii(string input)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(input))
|
||||||
|
return input;
|
||||||
|
|
||||||
|
// Normalize to FormD (decomposition)
|
||||||
|
string normalized = input.Normalize(NormalizationForm.FormD);
|
||||||
|
|
||||||
|
var sb = new StringBuilder();
|
||||||
|
|
||||||
|
foreach (char c in normalized)
|
||||||
|
{
|
||||||
|
var unicodeCategory = CharUnicodeInfo.GetUnicodeCategory(c);
|
||||||
|
// Skip non-spacing marks (accents, umlauts, etc.)
|
||||||
|
if (unicodeCategory != UnicodeCategory.NonSpacingMark)
|
||||||
|
{
|
||||||
|
sb.Append(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Normalize back to FormC and remove any remaining non-ASCII chars
|
||||||
|
string cleaned = sb.ToString().Normalize(NormalizationForm.FormC);
|
||||||
|
|
||||||
|
// Ensure only ASCII characters remain
|
||||||
|
var ascii = new StringBuilder();
|
||||||
|
foreach (char c in cleaned)
|
||||||
|
{
|
||||||
|
ascii.Append(c <= 127 ? c : '?'); // replace non-ASCII with '?' or remove
|
||||||
|
}
|
||||||
|
|
||||||
|
return ascii.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
public static FileStreamHttpResult RespondFileStream(this ControllerBase controller, Stream stream, string contentType, string fileName)
|
public static FileStreamHttpResult RespondFileStream(this ControllerBase controller, Stream stream, string contentType, string fileName)
|
||||||
{
|
{
|
||||||
stream.Seek(0, SeekOrigin.Begin);
|
stream.Seek(0, SeekOrigin.Begin);
|
||||||
|
|
||||||
controller.Response.Headers.Add("Content-Disposition", "inline; filename=" + fileName);
|
controller.Response.Headers.Add("Content-Disposition", "inline; filename=" + ToAscii(fileName.Normalize(NormalizationForm.FormD)));
|
||||||
return TypedResults.File(stream, contentType, fileName);
|
return TypedResults.File(stream, contentType, fileName);
|
||||||
}
|
}
|
||||||
public static FileStreamHttpResult RespondXlsx(this ControllerBase controller, XLWorkbook file, string fileName)
|
public static FileStreamHttpResult RespondXlsx(this ControllerBase controller, XLWorkbook file, string fileName)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user