简体   繁体   English

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

[英]Chrome modifying long file names when download them (using c# FileContentResult)

In a list of generated reports, have a link to download them. 在生成的报告列表中,有一个下载链接。 The contents of these reports are stored in the database. 这些报告的内容存储在数据库中。 When I perform the download a report, use the code below: 当我执行下载报告时,请使用以下代码:

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

In many cases, the filename exceeds 50 characters and when I do download the report using Google Chrome, the browser ignores the filename generated in the header and tries to save the file with the last parameter of the download link, which case the ID of report, example: the download link is http://appname.com/report/download/123 and return the browser is " 123.pdf " but should be " Relatório de probabilidade e risco de processos.pdf ". 在许多情况下,文件名超过50个字符,当我使用Google Chrome浏览器下载报告时,浏览器会忽略标头中生成的文件名,并尝试使用下载链接的最后一个参数(即报告ID)保存文件,例如:下载链接为http://appname.com/report/download/123 ,返回的浏览器为“ 123.pdf ”,但应为“ Relatóriode probabilidade e risco de processos.pdf ”。 But when I use Mozilla Firefox or IE, the problem does not occur. 但是,当我使用Mozilla Firefox或IE时,不会发生此问题。

Has anyone experienced this situation? 有人遇到过这种情况吗?

The problem is not related to the number of characters, but referring to special characters. 问题与字符数无关,而是指特殊字符。 I created a function that removes special characters and tried the filename. 我创建了一个删除特殊字符的函数,并尝试了文件名。

Function: 功能:

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

Corrected code: 更正的代码:

...

string reportName = StringUtils.RemoveSpecialCharacters(report.Title, true);

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

Thank you for your attention. 感谢您的关注。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM