gpt4 book ai didi

c# - Chrome 在下载时修改长文件名(使用 c# FileContentResult)

转载 作者:太空宇宙 更新时间:2023-11-03 13:51:31 25 4
gpt4 key购买 nike

在生成的报告列表中,有一个下载它们的链接。这些报告的内容存储在数据库中。当我执行下载报告时,使用以下代码:

return new FileContentResult (report.FileContents, mimeType)
{
FileDownloadName = report.Title + report.Extension
};

在许多情况下,文件名超过 50 个字符,当我使用 Google Chrome 下载报告时,浏览器会忽略 header 中生成的文件名,并尝试使用下载链接的最后一个参数保存文件,在这种情况下报告ID,例如:下载地址为http://appname.com/report/download/123并返回浏览器是“123.pdf”,但应该是“Relatório de probabilidade e risco de processos.pdf”。但是当我使用 Mozilla Firefox 或 IE 时,问题没有出现。

有没有人遇到过这种情况?

最佳答案

问题与字符数无关,而是指特殊字符。我创建了一个删除特殊字符的函数并尝试了文件名。

功能:

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

感谢您的关注。

关于c# - Chrome 在下载时修改长文件名(使用 c# FileContentResult),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13566928/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com