1
This commit is contained in:
@@ -5,7 +5,7 @@ using System.Configuration;
|
||||
using System.IO;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using System.Net;
|
||||
|
||||
namespace BLL
|
||||
{
|
||||
@@ -611,5 +611,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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user