lu
2025-04-12 813daab971b161ad4f40cbeb5b19b70a5b160aa2
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using DevExpress.Utils.DirectXPaint;
using DevExpress.XtraEditors;
using Gs.DevApp.ToolBox;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace Gs.DevApp.UserControl
{
    public partial class UCUpFile : DevExpress.XtraEditors.XtraUserControl
    {
        public UCUpFile()
        {
            InitializeComponent();
            //上传附件
            btnUpSelect.Click += BtnUpSelect_Click;
            btnUp.Click += BtnUp_Click;
        }
 
        /// <summary>
        /// 
        /// </summary>
        public string parentGuid
        {
            get;
            set;
        }
        /// <summary>
        /// 
        /// </summary>
        public string parentGroup
        {
            get;
            set;
        }
 
        /// <summary>
        ///     改变事件
        /// </summary>
        public event EventHandler UpChanged;
 
        #region 文件上传
 
        /// <summary>
        ///     选择上传文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnUpSelect_Click(object sender, EventArgs e)
        {
            var openFileDialog = new OpenFileDialog();
            // 设置初始目录
            openFileDialog.InitialDirectory = "c:\\";
            // 设置文件过滤选项,如:"文本文件 (*.txt)|*.txt|所有文件 (*.*)|*.*"
            openFileDialog.Filter = "文本文件 (*.txt)|*.txt|所有文件 (*.*)|*.*";
            // 设置标题
            openFileDialog.Title = "选择文件";
            // 显示对话框
            var result = openFileDialog.ShowDialog();
            // 确认用户没有取消操作
            if (result == DialogResult.OK)
            {
                // 获取选中的文件路径
                var filePath = openFileDialog.FileName;
                Console.WriteLine($"选中的文件: {filePath}");
                txtUpUrl.Text = filePath;
            }
        }
 
        /// <summary>
        ///     确定上传
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void BtnUp_Click(object sender, EventArgs e)
        {
            var WebApiUrl = ConfigurationSettings.AppSettings.Get("WebApiUrl");
            var address = WebApiUrl + "MesFile/UploadFile";
            var filePath = txtUpUrl.Text.Trim();
            if (string.IsNullOrEmpty(filePath))
            {
                Gs.DevApp.ToolBox.MsgHelper.ShowError("请选择你需要上传的文件!");
                return;
            }
            var apiUrl = address; // 替换为你的API地址
            await UploadFileAsync(apiUrl, filePath);
            if (UpChanged != null) UpChanged(this, e);
        }
 
        private async Task UploadFileAsync(string url, string filePath)
        {
            string _parentGuid = parentGuid;
            using (var httpClient = new HttpClient())
                try
                {
                    using (var form = new MultipartFormDataContent())
                    {
                        httpClient.DefaultRequestHeaders.Add("token",
                            UtilityHelper.GetBasicAuthTicket());
                        using (var fs = File.OpenRead(filePath))
                        using (var streamContent = new StreamContent(fs))
                        {
                            form.Add(streamContent, "file", _parentGuid + "~" + Path.GetFileName(filePath) + "~" + parentGroup);
                            var response = await httpClient.PostAsync(url, form);
                            var responseString =
                                await response.Content.ReadAsStringAsync();
                            //  MessageBox.Show(responseString);
                            txtUpUrl.Text = "";
                        }
                    }
                }
                catch (Exception ex)
                {
                    Gs.DevApp.ToolBox.MsgHelper.ShowError(ex.Message);
 
                }
 
        }
 
        #endregion
    }
}