public bool Put(string localPath, string remoteDir) { try { //TransferOperationResult transferOperationResult = _session.PutFiles(localPath, remoteDir, false); //return transferOperationResult.IsSuccess; var args = _session.PutFileToDirectory(localPath, remoteDir, false); return(args.Error == null); } catch (Exception ex) { return(false); } }
//单线程即可 public static Tuple <int, string> m_fSendFile(m_cRecModel m_mRecModel, string m_sRemoteDirectory, bool m_bTest, LogTyper lt = LogTyper.LogLogger) { int status = 0; string msg = string.Empty; try { ///链接(S)FTP SessionOptions sessionOptions = new SessionOptions(); sessionOptions.Protocol = (Protocol)(int.Parse(m_cConfigConstants.m_sProtocol)); sessionOptions.HostName = m_cConfigConstants.m_sIP; sessionOptions.PortNumber = int.Parse(m_cConfigConstants.m_sPort); sessionOptions.UserName = m_cConfigConstants.m_sUa; sessionOptions.FtpMode = (FtpMode)(int.Parse(m_cConfigConstants.m_sMode)); if (!string.IsNullOrWhiteSpace(m_cConfigConstants.m_sPwd)) { sessionOptions.Password = m_cConfigConstants.m_sPwd; } if (!string.IsNullOrWhiteSpace(m_cConfigConstants.m_sKey)) { sessionOptions.SshHostKeyFingerprint = m_cConfigConstants.m_sKey; } ///放入测试 using (WinSCP.Session session = new WinSCP.Session()) { session.Open(sessionOptions); TransferEventArgs transferEventArgs; TransferOptions transferOptions = new TransferOptions(); transferOptions.TransferMode = TransferMode.Binary; //是否需要创建文件夹,逐级创建 if (!string.IsNullOrWhiteSpace(m_sRemoteDirectory) && !session.FileExists(RemotePath.EscapeFileMask(m_sRemoteDirectory))) { //查找出所有目录 string[] m_lDirectory = m_sRemoteDirectory.Split('/'); string m_sPrefixMulu = string.Empty; foreach (string m_sMulu in m_lDirectory) { if (!string.IsNullOrWhiteSpace(m_sMulu)) { m_sPrefixMulu += $"/{m_sMulu}"; if (!session.FileExists(RemotePath.EscapeFileMask(m_sPrefixMulu))) { session.CreateDirectory(RemotePath.EscapeFileMask(m_sPrefixMulu)); Log.Instance.Debug($"逐级创建(S)FTP目录:“{m_sPrefixMulu}”", lt); } } } } //判断文件是否已经上传 if (!session.FileExists(RemotePath.EscapeFileMask($"{m_sRemoteDirectory}/{System.IO.Path.GetFileName(m_mRecModel.mimiFile)}"))) { //如果为测试,文件不进行删除 transferEventArgs = session.PutFileToDirectory(m_mRecModel.mimiFile, RemotePath.EscapeFileMask(m_sRemoteDirectory), !m_bTest, transferOptions); string m_sErrMsg = string.Join(",", transferEventArgs?.Error?.Message); if (!string.IsNullOrWhiteSpace(m_sErrMsg)) { Log.Instance.Debug(m_sErrMsg, lt); status = 1; msg = m_sErrMsg; } else { status = 0; msg = "录音上传(S)FTP成功"; } } else { status = 0; msg = "录音已存在,略过上传(S)FTP"; if (!m_bTest) { Log.Instance.Debug($"{m_mRecModel.Id}:“{msg}”", lt); } } } } catch (Exception ex) { status = 1; msg = ex.Message; //如果有误 if (!m_bTest) { m_cSQL.m_fSetActionState(m_mRecModel.Id, "3", $"上传(S)FTP错误:{msg}", lt); } //打印一下错误,处理一下此处导致计划任务无法继续的问题 Log.Instance.Debug(ex, lt); } return(new Tuple <int, string>(status, msg)); }