Forráskód Böngészése

维修上传图片

yhluo 2 hónapja
szülő
commit
8d36afbee0
1 módosított fájl, 126 hozzáadás és 0 törlés
  1. 126 0
      UAS_MES_BG/FunctionCode/Make/Make_Repair.cs

+ 126 - 0
UAS_MES_BG/FunctionCode/Make/Make_Repair.cs

@@ -3,6 +3,8 @@ using System.Collections;
 using System.Collections.Generic;
 using System.Data;
 using System.Drawing;
+using System.IO;
+using System.Net;
 using System.Text;
 using System.Windows.Forms;
 using UAS_MES_NEW.CustomControl.DataGrid_View;
@@ -563,6 +565,31 @@ namespace UAS_MES_NEW.Make
                 OperatResult.AppendText(">>" + ErrorMessage + "未维护不良原因\n", Color.Red);
                 return;
             }
+            DialogResult result = MessageBox.Show("是否需要上传图片", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
+            if(result == DialogResult.Yes)
+            {
+                using (OpenFileDialog openFileDialog = new OpenFileDialog())
+                {
+                    openFileDialog.Multiselect = true;
+                    openFileDialog.Title = "选择图片";
+                    openFileDialog.Filter = "图片文件|*.jpg;*.jpeg;*.png;*.bmp;*.gif;*.tiff;*.ico";
+
+                    if (openFileDialog.ShowDialog() == DialogResult.OK)
+                    {
+                        foreach (string path in openFileDialog.FileNames)
+                        {
+                           if (UploadFileToFtp(path, ms_sncode.Text.Trim()))
+                           {
+                                dh.ExecuteSql($@"INSERT INTO steptestdetail( std_id, std_makecode, std_sn, std_class, 
+                                                std_testresult, std_value1) 
+                                                VALUES( steptestdetail_seq.NEXTVAL, '{ms_makecode.Text}', '{ms_sncode.Text.Trim()}', '维修图片', 
+                                                'http://192.168.1.92:8088/ftp/mes/Picture/{ms_sncode.Text.Trim()}/{Path.GetFileName(path)}', nvl(std_value1, 0) + 1)", "insert");
+                           }
+                        }
+                    }
+                }
+            }
+
             //如果岗位资源对应工序的版面不为空的时候
             //if (dh.getFieldDataByCondition("makeserial left join step on ms_stepcode=st_code", "st_table", "ms_id='" + msid + "'").ToString() != "")
             //{
@@ -876,8 +903,10 @@ namespace UAS_MES_NEW.Make
         private void mbl_loc_KeyDown(object sender, KeyEventArgs e)
         {
             if (e.KeyCode == Keys.Enter)
+            {
                 QueryBadLocal();
                 SaveBadLocation_Click(sender, e);
+            }
         }
 
         private void QueryBadLocal()
@@ -1216,5 +1245,102 @@ namespace UAS_MES_NEW.Make
                 }
             }
         }
+
+        public bool UploadFileToFtp(string localFilePath,string Sn)
+        {
+            //string ftpServer = "ftp://10.8.0.208:21/mes/Picture/";
+            string ftpServer = "ftp://192.168.1.92:21/mes/Picture/";
+            string username = "vsftpd";
+            string password = "vsftpd3cd79018fl";
+
+            /*string currentDate = DateTime.Now.ToString("yyyyMMdd");
+            string ftpFullPath = $"{ftpServer.TrimEnd('/')}/{currentDate}";*/
+
+            string ftpFullPath = $"{ftpServer.TrimEnd('/')}/{Sn}";
+            string outResult = CreateFtpDirectoryIfNotExists(ftpFullPath, username, password);
+            if (outResult.Substring(0, 2) == "NG")
+            {
+                OperatResult.AppendText($">{outResult}\n", Color.Red);
+                return false;
+            }
+
+            string remoteFileName = Path.GetFileName(localFilePath);
+            string uri = $"{ftpFullPath}/{remoteFileName}";
+
+            try
+            {
+                var request = (FtpWebRequest)WebRequest.Create(uri);
+                request.Method = WebRequestMethods.Ftp.UploadFile;
+                request.Credentials = new NetworkCredential(username, password);
+                request.UsePassive = true;
+                request.UseBinary = true;
+                request.KeepAlive = false;
+
+                using (var fileStream = File.OpenRead(localFilePath))
+                using (var requestStream = request.GetRequestStream())
+                {
+                    byte[] buffer = new byte[4096];
+                    int bytesRead;
+                    while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) > 0)
+                    {
+                        requestStream.Write(buffer, 0, bytesRead);
+                    }
+                }
+
+                using (var response = (FtpWebResponse)request.GetResponse())
+                {
+                    OperatResult.AppendText($">文件: {localFilePath}上传成功,状态{response.StatusDescription}\n", Color.Green);
+                    return true;
+                }
+            }
+            catch (WebException ex)
+            {
+                if (ex.Response is FtpWebResponse response)
+                {
+                    OperatResult.AppendText($">FTP 错误码: {(int)response.StatusCode} - {response.StatusDescription}\n", Color.Red);
+                }
+                else
+                {
+                    OperatResult.AppendText($"Web异常: {ex.Message}", Color.Red);
+                }
+                return false;
+            }
+            catch (Exception ex)
+            {
+                OperatResult.AppendText($"上传失败: {ex.Message}", Color.Red);
+                return false;
+            }
+        }
+
+
+        private string CreateFtpDirectoryIfNotExists(string ftpDirectoryPath, string username, string password)
+        {
+            try
+            {
+                FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpDirectoryPath);
+                request.Method = WebRequestMethods.Ftp.MakeDirectory;
+                request.Credentials = new NetworkCredential(username, password);
+                request.UsePassive = true;
+                request.UseBinary = true;
+                request.KeepAlive = false;
+
+                using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
+                {
+                    response.Close();
+                    return "OK,目录创建成功: " + response.StatusDescription;
+                }
+            }
+            catch (WebException ex)
+            {
+                if (ex.Response is FtpWebResponse response && response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
+                {
+                    return "OK,目录已存在: " + response.StatusDescription;
+                }
+                else
+                {
+                    return "NG,创建目录时发生错误: " + ex.Message;
+                }
+            }
+        }
     }
 }