schangxiang@126.com
2025-11-04 f5ed29dc26c7cd952d56ec5721a2efc43cd25992
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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;
        }
    }
}