From ae27635bd92120d0dd975d8480c344162610219a Mon Sep 17 00:00:00 2001
From: lg <999544862qq.com>
Date: 星期二, 17 九月 2024 10:35:01 +0800
Subject: [PATCH] 文件上传

---
 DevApp/Gs.DevApp/DevFrm/Sys/EasyCode.cs |  141 ++++++++++++++++++++++++++++++++++------------
 1 files changed, 104 insertions(+), 37 deletions(-)

diff --git a/DevApp/Gs.DevApp/DevFrm/Sys/EasyCode.cs b/DevApp/Gs.DevApp/DevFrm/Sys/EasyCode.cs
index ae9b595..1b54b57 100644
--- a/DevApp/Gs.DevApp/DevFrm/Sys/EasyCode.cs
+++ b/DevApp/Gs.DevApp/DevFrm/Sys/EasyCode.cs
@@ -5,7 +5,10 @@
 using System.Data;
 using System.Data.SqlClient;
 using System.Drawing;
+using System.IO;
 using System.Linq;
+using System.Net;
+using System.Net.Http;
 using System.Text;
 using System.Threading.Tasks;
 using System.Windows.Forms;
@@ -17,47 +20,30 @@
         public EasyCode()
         {
             InitializeComponent();
-            this.txt_sqlconn.Text = "Data Source=2kn4sh70028.vicp.fun,26144;Initial Catalog=gs_Mes;User ID=sa;Password =admin110";
+            //this.txt_sqlconn.Text = "Data Source=2kn4sh70028.vicp.fun,26144;Initial Catalog=gs_Mes;User ID=sa;Password =admin110";
+            this.txt_sqlconn.Text = "Data Source=.;Initial Catalog=gs_Mes;User ID=sa;Password =admin110";
             getTable();
-        }
-        /// <summary>
-        /// 璇诲彇鏁版嵁搴撹〃
-        /// </summary>
-        private void getTable()
-        {
-            string strConn = txt_sqlconn.Text.Trim();
-            DataSet dset = new DataSet();
-            using (SqlConnection connection = new SqlConnection(strConn))
-            {
-                connection.Open();
-                SqlCommand cmd = new SqlCommand();
-                cmd.Connection = connection;
-                cmd.CommandText = "SELECT '--璇烽�夋嫨--' as TABLE_NAME union all SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' order by TABLE_NAME desc";
-                using (SqlDataAdapter dt = new SqlDataAdapter(cmd))
-                {
-                    dt.Fill(dset, "0");
-                }
-            }
-            if (dset != null && dset.Tables.Count > 0)
-            {
-                txt_table.DataSource = dset.Tables[0];
-                txt_table.DisplayMember = "TABLE_NAME";
-                txt_table.ValueMember = "TABLE_NAME";
-                txt_category.SelectedIndex = txt_table.SelectedIndex = 0;
-            }
+            this.btnToPath.Click += BtnToPath_Click;
+            this.btnUpSelect.Click += BtnUpSelect_Click;
+            this.btnUp.Click += BtnUp_Click;
         }
 
+      
         /// <summary>
         /// 閫夋嫨鐢熸垚璺緞
         /// </summary>
         /// <param name="sender"></param>
         /// <param name="e"></param>
-        private void btnToPath_Click(object sender, EventArgs e)
+        private void BtnToPath_Click(object sender, EventArgs e)
         {
             string _GetfilePath = FileSelector.BrowseFilePathForSave();
             txtCreateDBPath.Text = _GetfilePath;
         }
-
+        /// <summary>
+        /// 纭畾鐢熸垚
+        /// </summary>
+        /// <param name="sender"></param>
+        /// <param name="e"></param>
         private void btnOk_Click(object sender, EventArgs e)
         {
             if (txt_sqlconn.Text.Trim() == "")
@@ -87,6 +73,32 @@
             ReadResxFile(dt, _strName);
             string path = txtCreateDBPath.Text;
             lbMsg.Text = "鐢熸垚鎴愬姛锛岃矾寰勶細" + path;
+        }
+        /// <summary>
+        /// 璇诲彇鏁版嵁搴撹〃
+        /// </summary>
+        private void getTable()
+        {
+            string strConn = txt_sqlconn.Text.Trim();
+            DataSet dset = new DataSet();
+            using (SqlConnection connection = new SqlConnection(strConn))
+            {
+                connection.Open();
+                SqlCommand cmd = new SqlCommand();
+                cmd.Connection = connection;
+                cmd.CommandText = "SELECT '--璇烽�夋嫨--' as TABLE_NAME union all SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' order by TABLE_NAME desc";
+                using (SqlDataAdapter dt = new SqlDataAdapter(cmd))
+                {
+                    dt.Fill(dset, "0");
+                }
+            }
+            if (dset != null && dset.Tables.Count > 0)
+            {
+                txt_table.DataSource = dset.Tables[0];
+                txt_table.DisplayMember = "TABLE_NAME";
+                txt_table.ValueMember = "TABLE_NAME";
+                txt_category.SelectedIndex = txt_table.SelectedIndex = 0;
+            }
         }
         /// <summary>
         /// 璇诲彇鍒楀悕
@@ -293,17 +305,72 @@
             sw.Close();
             sw.Dispose();
         }
-
-        private void btnToPath_Click_1(object sender, EventArgs e)
-        {
-            string _GetfilePath = FileSelector.BrowseFilePathForSave();
-            txtCreateDBPath.Text = _GetfilePath;
-        }
-
+ 
+        /// <summary>
+        /// 鎶ヨ〃璁捐
+        /// </summary>
+        /// <param name="sender"></param>
+        /// <param name="e"></param>
         private void button1_Click(object sender, EventArgs e)
         {
             EasyRpt frm = new EasyRpt("001");
             frm.Show();
         }
+
+        /// <summary>
+        /// 閫夋嫨涓婁紶鏂囦欢
+        /// </summary>
+        /// <param name="sender"></param>
+        /// <param name="e"></param>
+        private void BtnUpSelect_Click(object sender, EventArgs e)
+        {
+            OpenFileDialog openFileDialog = new OpenFileDialog();
+            // 璁剧疆鍒濆鐩綍
+            openFileDialog.InitialDirectory = "c:\\";
+            // 璁剧疆鏂囦欢杩囨护閫夐」锛屽锛�"鏂囨湰鏂囦欢 (*.txt)|*.txt|鎵�鏈夋枃浠� (*.*)|*.*"
+            openFileDialog.Filter = "鏂囨湰鏂囦欢 (*.txt)|*.txt|鎵�鏈夋枃浠� (*.*)|*.*";
+            // 璁剧疆鏍囬
+            openFileDialog.Title = "閫夋嫨鏂囦欢";
+            // 鏄剧ず瀵硅瘽妗�
+            DialogResult result = openFileDialog.ShowDialog();
+            // 纭鐢ㄦ埛娌℃湁鍙栨秷鎿嶄綔
+            if (result == DialogResult.OK)
+            {
+                // 鑾峰彇閫変腑鐨勬枃浠惰矾寰�
+                string filePath = openFileDialog.FileName;
+                Console.WriteLine($"閫変腑鐨勬枃浠�: {filePath}");
+                txtUpUrl.Text = filePath;
+            }
+           
+        }
+
+        private async  void BtnUp_Click(object sender, EventArgs e)
+        {
+            string WebApiUrl = System.Configuration.ConfigurationSettings.AppSettings.Get("WebApiUrl").ToString();
+            string address = WebApiUrl + "Upload/UploadFile";
+            string filePath = txtUpUrl.Text.Trim();
+            string apiUrl = address; // 鏇挎崲涓轰綘鐨凙PI鍦板潃
+            await UploadFileAsync(apiUrl, filePath);
+        }
+
+        private async Task UploadFileAsync(string url, string filePath)
+        {
+            using (var httpClient = new HttpClient())
+            using (var form = new MultipartFormDataContent())
+            {
+                httpClient.DefaultRequestHeaders.Add("token", ToolBox.UtilityHelper.GetBasicAuthTicket());
+                using (var fs = File.OpenRead(filePath))
+                using (var streamContent = new StreamContent(fs))
+                {
+                    form.Add(streamContent, "file", Path.GetFileName(filePath));
+                    var response = await httpClient.PostAsync(url, form);
+                    string responseString = await response.Content.ReadAsStringAsync();
+                    MessageBox.Show(responseString);
+                }
+            }
+        }
     }
-}
\ No newline at end of file
+    
+}
+
+

--
Gitblit v1.9.3