This commit is contained in:
geh
2026-03-23 15:22:29 +08:00
parent 94584d242b
commit c38590add8
173 changed files with 19679 additions and 4413 deletions
@@ -3,6 +3,7 @@ using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
@@ -611,5 +612,46 @@ namespace BLL
}
#endregion
/// <summary>
/// 下载并保存文件
/// </summary>
/// <param name="webUrl"></param>
/// <param name="fileUrl"></param>
/// <returns></returns>
public static byte[] SafeDownloadHeadImageAsync(string webUrl, string fileUrl)
{
try
{
if (string.IsNullOrEmpty(fileUrl))
return null;
// 构造完整的远程文件 URL
string fullRemoteUrl = $"{webUrl.TrimEnd('/')}/{fileUrl.TrimStart('/')}";
// 构造本地文件路径
string localFilePath = Path.Combine(ConfigurationManager.AppSettings["localRoot"], fileUrl);
// 确保本地目录存在
string directoryPath = Path.GetDirectoryName(localFilePath);
if (!string.IsNullOrEmpty(directoryPath) && !Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
}
//下载文件到本地
using (WebClient client = new WebClient())
{
byte[] fileData = client.DownloadData(fullRemoteUrl);
// 将文件数据写入本地文件
File.WriteAllBytes(localFilePath, fileData);
return fileData;
}
}
catch (Exception ex)
{
BLL.ErrLogInfo.WriteLog($"下载头像失败: {fileUrl}, 错误: {ex.Message}");
return null;
}
}
}
}