diff --git a/DSWeb/Areas/Dispatch/Controllers/DispatchController.cs b/DSWeb/Areas/Dispatch/Controllers/DispatchController.cs index b77fc6f..d82ab9c 100644 --- a/DSWeb/Areas/Dispatch/Controllers/DispatchController.cs +++ b/DSWeb/Areas/Dispatch/Controllers/DispatchController.cs @@ -1236,6 +1236,7 @@ namespace DSWeb.Areas.Dispatch.Controllers var SftpUserName = WebConfigurationManager.AppSettings["SWISftpUserName"].ToString(); var SftpPassword = WebConfigurationManager.AppSettings["SWISftpPassword"].ToString(); var SftpPath= WebConfigurationManager.AppSettings["SWISftpPath"].ToString(); + var SftpSiPath = WebConfigurationManager.AppSettings["SWISftpSiPath"].ToString(); var str = ""; try @@ -1263,22 +1264,240 @@ namespace DSWeb.Areas.Dispatch.Controllers TransferOptions transferOptions = new TransferOptions(); transferOptions.TransferMode = TransferMode.Binary; - transferOptions.PreserveTimestamp = true; + transferOptions.PreserveTimestamp = false; TransferOperationResult transferResult; - transferResult = session.PutFiles(filename, SftpPath + name, false, transferOptions); + transferResult = session.PutFiles(filename, SftpPath, false, transferOptions); transferResult.Check(); - if (transferResult.IsSuccess) - { + //if (transferResult.IsSuccess) + //{ foreach (TransferEventArgs transfer in transferResult.Transfers) { } + //} + //else { + // result.Success = false; + // result.Message = "传输过程出现错误"; + + //} + + if (session != null) + { + session.Dispose(); + } + // 你的代码 + } + + + result.Success = true; + result.Message = "上传成功!"; + } + catch (Exception E) + { + result.Success = false; + result.Message = E.Message; + } + + + var jsonRespose2 = new JsonResponse { Success = result.Success, Message = result.Message }; + return new ContentResult() { Content = DSWeb.MvcShipping.Helper.JsonConvert.Serialize(jsonRespose2) }; + } + [HttpPost] + public ContentResult SftpSiFilesSWIJF() + { + var result = new DBResult(); + var jsonRespose = new JsonResponse { Success = false, Message = "" }; + + if (Request.Files.Count != 1) + { + jsonRespose.Success = false; + jsonRespose.Message = "请选择上传的文件"; + return new ContentResult() { Content = DSWeb.MvcShipping.Helper.JsonConvert.Serialize(jsonRespose) }; + } + + var file = Request.Files["filename"]; + if (file == null) + { + jsonRespose.Success = false; + jsonRespose.Message = "上传文件发生未知错误,请重新上传"; + return new ContentResult() { Content = DSWeb.MvcShipping.Helper.JsonConvert.Serialize(jsonRespose) }; + } + var name = Path.GetFileName(file.FileName); + var path = Server.MapPath("../../UploadFiles"); + + if (!Directory.Exists(path)) + { + Directory.CreateDirectory(path); + } + string filename = path + "\\" + name; + + if (System.IO.File.Exists(filename)) + { + System.IO.File.Delete(filename); + } + file.SaveAs(filename); + var keypath = Server.MapPath("../../ExpFiles"); + string keyfile = keypath + "\\SWIPrivate.ppk"; + var SshHostKeyFingerprint = WebConfigurationManager.AppSettings["SWIJFSshHostKeyFingerprint"].ToString(); + if (string.IsNullOrEmpty(SshHostKeyFingerprint)) SshHostKeyFingerprint = "ssh-rsa 2048 EcxSghC0Z/Vm+FO7cHEJspEPdB6e9hb12zOPQgMMjUk"; + var SftpServier = WebConfigurationManager.AppSettings["SWISftpServier"].ToString(); + var SftpPort = WebConfigurationManager.AppSettings["SWISftpPort"].ToString(); + var SftpUserName = WebConfigurationManager.AppSettings["SWISftpUserName"].ToString(); + var SftpPassword = WebConfigurationManager.AppSettings["SWISftpPassword"].ToString(); + var SftpPath = WebConfigurationManager.AppSettings["SWISftpPath"].ToString(); + var SftpSiPath = WebConfigurationManager.AppSettings["SWISftpSiPath"].ToString(); + + var str = ""; + try + { + // 配置会话选项 + SessionOptions sessionOptions = new SessionOptions + { + Protocol = Protocol.Sftp, + HostName = SftpServier, + PortNumber = Convert.ToInt32(SftpPort), + UserName = SftpUserName, + Password = SftpPassword, + SshHostKeyFingerprint = SshHostKeyFingerprint, + + SshPrivateKeyPath = keyfile, + + PrivateKeyPassphrase = SftpPassword, + }; + + sessionOptions.AddRawSettings("KEX", "rsa,ecdh,dh-gex-sha1,dh-group14-sha1,WARN,dh-group1-sha1"); + + using (Session session = new Session()) + { + session.Open(sessionOptions); + + TransferOptions transferOptions = new TransferOptions(); + transferOptions.TransferMode = TransferMode.Binary; + transferOptions.PreserveTimestamp = false; + TransferOperationResult transferResult; + transferResult = session.PutFiles(filename, SftpSiPath, false, transferOptions); + transferResult.Check(); + //if (transferResult.IsSuccess) + //{ + + foreach (TransferEventArgs transfer in transferResult.Transfers) + { + } + //} + //else { + // result.Success = false; + // result.Message = "传输过程出现错误"; + + //} + + if (session != null) + { + session.Dispose(); } - else { - result.Success = false; - result.Message = "传输过程出现错误"; + // 你的代码 + } + + + result.Success = true; + result.Message = "上传成功!"; + } + catch (Exception E) + { + result.Success = false; + result.Message = E.Message; + } + + + var jsonRespose2 = new JsonResponse { Success = result.Success, Message = result.Message }; + return new ContentResult() { Content = DSWeb.MvcShipping.Helper.JsonConvert.Serialize(jsonRespose2) }; + } + + [HttpPost] + public ContentResult SftpVgmFilesSWIJF() + { + var result = new DBResult(); + var jsonRespose = new JsonResponse { Success = false, Message = "" }; + + if (Request.Files.Count != 1) + { + jsonRespose.Success = false; + jsonRespose.Message = "请选择上传的文件"; + return new ContentResult() { Content = DSWeb.MvcShipping.Helper.JsonConvert.Serialize(jsonRespose) }; + } + + var file = Request.Files["filename"]; + if (file == null) + { + jsonRespose.Success = false; + jsonRespose.Message = "上传文件发生未知错误,请重新上传"; + return new ContentResult() { Content = DSWeb.MvcShipping.Helper.JsonConvert.Serialize(jsonRespose) }; + } + var name = Path.GetFileName(file.FileName); + var path = Server.MapPath("../../UploadFiles"); + + if (!Directory.Exists(path)) + { + Directory.CreateDirectory(path); + } + string filename = path + "\\" + name; + + if (System.IO.File.Exists(filename)) + { + System.IO.File.Delete(filename); + } + file.SaveAs(filename); + var keypath = Server.MapPath("../../ExpFiles"); + string keyfile = keypath + "\\SWIPrivate.ppk"; + var SshHostKeyFingerprint = WebConfigurationManager.AppSettings["SWIJFSshHostKeyFingerprint"].ToString(); + if (string.IsNullOrEmpty(SshHostKeyFingerprint)) SshHostKeyFingerprint = "ssh-rsa 2048 EcxSghC0Z/Vm+FO7cHEJspEPdB6e9hb12zOPQgMMjUk"; + var SftpServier = WebConfigurationManager.AppSettings["SWISftpServier"].ToString(); + var SftpPort = WebConfigurationManager.AppSettings["SWISftpPort"].ToString(); + var SftpUserName = WebConfigurationManager.AppSettings["SWISftpUserName"].ToString(); + var SftpPassword = WebConfigurationManager.AppSettings["SWISftpPassword"].ToString(); + var SftpVgmPath = WebConfigurationManager.AppSettings["SWISftpVgmPath"].ToString(); + var str = ""; + try + { + // 配置会话选项 + SessionOptions sessionOptions = new SessionOptions + { + Protocol = Protocol.Sftp, + HostName = SftpServier, + PortNumber = Convert.ToInt32(SftpPort), + UserName = SftpUserName, + Password = SftpPassword, + SshHostKeyFingerprint = SshHostKeyFingerprint, + + SshPrivateKeyPath = keyfile, + + PrivateKeyPassphrase = SftpPassword, + }; + + sessionOptions.AddRawSettings("KEX", "rsa,ecdh,dh-gex-sha1,dh-group14-sha1,WARN,dh-group1-sha1"); + + using (Session session = new Session()) + { + session.Open(sessionOptions); + + TransferOptions transferOptions = new TransferOptions(); + transferOptions.TransferMode = TransferMode.Binary; + transferOptions.PreserveTimestamp = false; + TransferOperationResult transferResult; + transferResult = session.PutFiles(filename, SftpVgmPath, false, transferOptions); + transferResult.Check(); + //if (transferResult.IsSuccess) + //{ + + foreach (TransferEventArgs transfer in transferResult.Transfers) + { } + //} + //else { + // result.Success = false; + // result.Message = "传输过程出现错误"; + + //} if (session != null) { @@ -1302,6 +1521,7 @@ namespace DSWeb.Areas.Dispatch.Controllers return new ContentResult() { Content = DSWeb.MvcShipping.Helper.JsonConvert.Serialize(jsonRespose2) }; } + [HttpPost] public ContentResult SftpFilesYT() { diff --git a/DSWeb/Properties/PublishProfiles/配置文件1.pubxml b/DSWeb/Properties/PublishProfiles/配置文件1.pubxml index ad78fef..87e545d 100644 --- a/DSWeb/Properties/PublishProfiles/配置文件1.pubxml +++ b/DSWeb/Properties/PublishProfiles/配置文件1.pubxml @@ -9,9 +9,9 @@ Release Any CPU - True - False - D:\DS7JIEKOUBAK - True + true + false + D:\DS7发布\DS7JIEKOU + true \ No newline at end of file