이 문제는 문자 수와 관련이 없지만 특수 문자를 참조하면 발생합니다. 특수 문자를 제거하고 파일 이름을 시도한 함수를 만들었습니다.
기능 :
/// <summary>
/// Remove characters from string
/// </summary>
public static string RemoveSpecialCharacters(string text, bool allowSpace)
{
var normalizedString = text;
// Prepare the symbol table.
var symbolTable = new Dictionary<char, char[]>();
symbolTable.Add('a', new char[] { 'à', 'á', 'ä', 'â', 'ã' });
symbolTable.Add('c', new char[] { 'ç' });
symbolTable.Add('e', new char[] { 'è', 'é', 'ë', 'ê' });
symbolTable.Add('i', new char[] { 'ì', 'í', 'ï', 'î' });
symbolTable.Add('o', new char[] { 'ò', 'ó', 'ö', 'ô', 'õ' });
symbolTable.Add('u', new char[] { 'ù', 'ú', 'ü', 'û' });
// Replaces the symbols.
foreach (var key in symbolTable.Keys)
{
foreach (var symbol in symbolTable[key])
{
normalizedString = normalizedString.Replace(symbol, key);
}
}
// Remove the other special characters.
if (allowSpace)
normalizedString = System.Text.RegularExpressions.Regex.Replace(normalizedString, @"[^0-9a-zA-Z.-_\s]+?", string.Empty);
else
normalizedString = System.Text.RegularExpressions.Regex.Replace(normalizedString, @"[^0-9a-zA-Z.-_]+?", string.Empty);
return normalizedString;
}
수정 코드 :
...
string reportName = StringUtils.RemoveSpecialCharacters(report.Title, true);
return new FileContentResult(report.FileContents, mimeType)
{
FileDownloadName = reportName + report.Extension
};
이 관심을 가져 주셔서 감사합니다.