using System;
|
using System.Collections.Generic;
|
using System.Drawing;
|
using System.IO;
|
using System.Linq;
|
using System.Net;
|
using System.Text;
|
using System.Threading.Tasks;
|
using System.Windows.Media;
|
using XCommon.Log;
|
|
namespace XCommon.file
|
{
|
/// <summary>
|
/// 导出文件
|
/// </summary>
|
public class ImportFile
|
{
|
/// <summary>
|
/// 下载服务器文件至客户端
|
/// </summary>
|
/// <param name="uri">被下载的文件地址</param>
|
/// <param name="savePath">另存放的目录</param>
|
public static bool Download(string uri, string savePath)
|
{
|
string fileName; //被下载的文件名
|
if (uri.IndexOf("\\") > -1)
|
{
|
fileName = uri.Substring(uri.LastIndexOf("\\") + 1);
|
}
|
else
|
{
|
fileName = uri.Substring(uri.LastIndexOf("/") + 1);
|
}
|
|
|
if (!savePath.EndsWith("/") && !savePath.EndsWith("\\"))
|
{
|
savePath = savePath + "/";
|
}
|
|
savePath += DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + fileName; //另存为的绝对路径+文件名
|
|
WebClient client = new WebClient();
|
try
|
{
|
client.DownloadFile(uri, savePath);
|
}
|
catch (Exception ex)
|
{
|
LoggerHelper.ErrorLog($"ImportFile.Download 失败:{ex.Message}");
|
return false;
|
}
|
return true;
|
}
|
|
|
/// <summary>
|
/// 获取图片
|
/// </summary>
|
/// <param name="filePath">图片路径</param>
|
/// <returns></returns>
|
public static ImageSource GetImage(string filePath)
|
{
|
byte[] imageBytes = Convert.FromBase64String(filePath);
|
// 读入MemoryStream对象
|
MemoryStream ms = new System.IO.MemoryStream(imageBytes);
|
ImageSourceConverter imageSourceConverter = new ImageSourceConverter();
|
// 转成图片
|
ImageSource source = (ImageSource)imageSourceConverter.ConvertFrom(ms);
|
return source;
|
}
|
|
/// <summary>
|
/// 获取图片Base64字符串
|
/// </summary>
|
/// <param name="filePath">图片路径</param>
|
/// <returns></returns>
|
public static string GetImageByString(string filePath)
|
{
|
Bitmap picture = new Bitmap(filePath);
|
MemoryStream ms = new MemoryStream();
|
picture.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
|
byte[] arr = new byte[ms.Length];
|
ms.Position = 0;
|
ms.Read(arr, 0, (int)ms.Length);
|
ms.Close();
|
string pic = Convert.ToBase64String(arr);
|
return pic;
|
}
|
}
|
}
|