You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
39 lines
1.4 KiB
39 lines
1.4 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace AIK.Common.WebHelper
|
|
{
|
|
public static class UrlHelper
|
|
{
|
|
/// <summary>
|
|
/// 从完整URL中提取文件名(保留URL编码,如 %28 不会转成 ()
|
|
/// </summary>
|
|
/// <param name="url">完整的下载链接</param>
|
|
/// <returns>URL编码的文件名,如 "20251126055829_721_%28K3_TOOL%291.2.9.bin"</returns>
|
|
public static string GetEncodedFileNameFromUrl(string url)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(url))
|
|
return string.Empty;
|
|
try
|
|
{
|
|
var uri = new Uri(url);
|
|
// Segments 是路径按 '/' 分割的数组,最后一个元素即为文件名(含原始编码)
|
|
string[] segments = uri.Segments;
|
|
if (segments.Length == 0)
|
|
return string.Empty;
|
|
|
|
string fileName = segments[segments.Length - 1];
|
|
// 移除末尾可能的斜杠(虽然文件名通常不会有)
|
|
return fileName.TrimEnd('/');
|
|
}
|
|
catch (UriFormatException)
|
|
{
|
|
// URL 格式无效时返回空或抛异常,根据需求调整
|
|
return string.Empty;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|