C# 如何使用c使文件名web安全

C# 如何使用c使文件名web安全,c#,asp.net,path,C#,Asp.net,Path,这与URL编码无关,更多的是与我注意到的一个问题有关,您可以在IIS上使用有效的文件名,例如test&test.jpg,但由于出现错误而无法下载。在windows中也有其他这样做的字符有效,但不适用于web 我的快速解决方案是在保存之前使用下面的正则表达式更改文件名 public static string MakeFileNameWebSafe(string fileNameIn) { string pattern = @"[^A-Za-z0-9. ]";

这与URL编码无关,更多的是与我注意到的一个问题有关,您可以在IIS上使用有效的文件名,例如test&test.jpg,但由于出现错误而无法下载。在windows中也有其他这样做的字符有效,但不适用于web

我的快速解决方案是在保存之前使用下面的正则表达式更改文件名

    public static string MakeFileNameWebSafe(string fileNameIn)
    {
        string pattern = @"[^A-Za-z0-9. ]";
        string safeFilename = System.Text.RegularExpressions.Regex.Replace(fileNameIn, pattern, string.Empty);
        if (safeFilename.StartsWith(".")) safeFilename = "noname" + safeFilename;

        return safeFilename;
    }

但是我想知道是否有更好的内置方式可以做到这一点。

如果你不想保留原始名称,也许你可以用guid替换名称?

如果你不想保留原始名称,也许你可以用guid替换名称?

内置我不知道

你可以做的是,就像你说的,扫描原始文件名并生成一个网络安全版本

对于此类网络安全版本,您可以使其在博客和博客类别中看起来像鼻涕虫,这些都是搜索引擎优化的:

仅小写字符 数字是允许的 破折号是允许的 空格将替换为破折号 除此之外不允许任何其他行为 也许你可以用&和替换-
所以test&test.jpg将转换为test和test.jpg。

内置,我不知道

你可以做的是,就像你说的,扫描原始文件名并生成一个网络安全版本

对于此类网络安全版本,您可以使其在博客和博客类别中看起来像鼻涕虫,这些都是搜索引擎优化的:

仅小写字符 数字是允许的 破折号是允许的 空格将替换为破折号 除此之外不允许任何其他行为 也许你可以用&和替换-
因此,test&test.jpg将转换为test和test.jpg。

我认为您指的是一个潜在危险的请求。从客户端检测到的路径值为%error,Asp.Net为包含可能表示跨站点脚本尝试的字符的路径抛出该错误:

有一篇关于如何解决这一问题的好文章:


我认为您所指的是潜在危险的请求。从客户端%错误检测到路径值,Asp.Net为包含可能表示跨站点脚本尝试的字符的路径抛出该错误:

有一篇关于如何解决这一问题的好文章:


回顾一下这个问题,因为它相当流行。尽管我会将我当前的解决方案发布到这里,为任何想要它的人提供各种重载

public static string MakeSafeFilename(string filename, string spaceReplace)
    {
        return MakeSafeFilename(filename, spaceReplace, false, false);
    }

    public static string MakeSafeUrlSegment(string text)
    {
        return MakeSafeUrlSegment(text, "-");
    }

    public static string MakeSafeUrlSegment(string text, string spaceReplace)
    {
        return MakeSafeFilename(text, spaceReplace, false, true);
    }

    public static string MakeSafeFilename(string filename, string spaceReplace, bool htmlDecode, bool forUrlSegment)
    {
        if (htmlDecode)
            filename = HttpUtility.HtmlDecode(filename);
        string pattern = forUrlSegment ? @"[^A-Za-z0-9_\- ]" : @"[^A-Za-z0-9._\- ]";
        string safeFilename = Regex.Replace(filename, pattern, string.Empty);

        safeFilename = safeFilename.Replace(" ", spaceReplace);

        return safeFilename;
    }

回顾一下这个问题,因为它相当流行。尽管我会将我当前的解决方案发布到这里,为任何想要它的人提供各种重载

public static string MakeSafeFilename(string filename, string spaceReplace)
    {
        return MakeSafeFilename(filename, spaceReplace, false, false);
    }

    public static string MakeSafeUrlSegment(string text)
    {
        return MakeSafeUrlSegment(text, "-");
    }

    public static string MakeSafeUrlSegment(string text, string spaceReplace)
    {
        return MakeSafeFilename(text, spaceReplace, false, true);
    }

    public static string MakeSafeFilename(string filename, string spaceReplace, bool htmlDecode, bool forUrlSegment)
    {
        if (htmlDecode)
            filename = HttpUtility.HtmlDecode(filename);
        string pattern = forUrlSegment ? @"[^A-Za-z0-9_\- ]" : @"[^A-Za-z0-9._\- ]";
        string safeFilename = Regex.Replace(filename, pattern, string.Empty);

        safeFilename = safeFilename.Replace(" ", spaceReplace);

        return safeFilename;
    }
这是我使用的一个:

public static string MakeFileNameWebSafe(string path, string replace, string other)
{
    var folder = System.IO.Path.GetDirectoryName(path);
    var name = System.IO.Path.GetFileNameWithoutExtension(path);
    var ext = System.IO.Path.GetExtension(path);
    if (name == null) return path;
    var allowed = @"a-zA-Z0-9" + replace + (other ?? string.Empty);
    name = System.Text.RegularExpressions.Regex.Replace(name.Trim(), @"[^" + allowed + "]", replace);
    name = System.Text.RegularExpressions.Regex.Replace(name, @"[" + replace + "]+", replace);
    if (name.EndsWith(replace)) name = name.Substring(0, name.Length - 1);
    return folder + name + ext;
}
这是我使用的一个:

public static string MakeFileNameWebSafe(string path, string replace, string other)
{
    var folder = System.IO.Path.GetDirectoryName(path);
    var name = System.IO.Path.GetFileNameWithoutExtension(path);
    var ext = System.IO.Path.GetExtension(path);
    if (name == null) return path;
    var allowed = @"a-zA-Z0-9" + replace + (other ?? string.Empty);
    name = System.Text.RegularExpressions.Regex.Replace(name.Trim(), @"[^" + allowed + "]", replace);
    name = System.Text.RegularExpressions.Regex.Replace(name, @"[" + replace + "]+", replace);
    if (name.EndsWith(replace)) name = name.Substring(0, name.Length - 1);
    return folder + name + ext;
}

我通常会这样做。Guid或DB行标识,但在本例中,我需要保留文件名。我以前从未遇到过这个问题。我想人们通常不会把&放在一个文件名中,但它最终肯定会发生:我通常会这样做。Guid或DB行标识,但在本例中,我需要保留文件名。我以前从未遇到过这个问题。我想人们通常不会把&放在一个文件名中,但它最终肯定会发生:投票率上升,尽管这里的所有答案似乎都假设为英语。RegExp[^a-zA-Z0-9]将过滤掉一个文件名,如СП́́ббб.txt俄罗斯人造卫星。所以也许[^\w\.\-]会更有效。也可以看到这篇关于匹配投票的帖子,尽管这里的答案似乎都是英语。RegExp[^a-zA-Z0-9]将过滤掉一个文件名,如СП́́ббб.txt俄罗斯人造卫星。所以也许[^\w\.\-]会更有效。也可以看到这篇关于匹配的文章