UpLoadFile.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using EnterpriseDT.Net.Ftp;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Web;
  6. /// <summary>
  7. /// Nameing 的摘要说明
  8. /// </summary>
  9. public class UpLoadFile
  10. {
  11. FTPConnection ftpConnection;
  12. public Boolean flag;
  13. public static string fileName1;
  14. public static int INV_ORG_ID;
  15. public UpLoadFile()
  16. {
  17. }
  18. //"192.168.0.187";
  19. #region 初始化文件传输ftp + void InitFtpConnect()
  20. /// <summary>
  21. /// 初始化文件传输ftp
  22. /// </summary>
  23. private void InitFtpConnect()
  24. {
  25. flag = false;
  26. if (this.ftpConnection == null)
  27. {
  28. this.ftpConnection = new FTPConnection();
  29. //this.ftpConnection.ParentControl = this.fQ;
  30. this.ftpConnection.StrictReturnCodes = true;
  31. this.ftpConnection.Timeout = 15000;
  32. //this.ftpConnection.ServerAddress = "120.76.96.55";// "192.168.0.187";//ftpInfo.FtpSeverIP;
  33. this.ftpConnection.ServerAddress = "101.37.27.113";// "192.168.0.187";//ftpInfo.FtpSeverIP;
  34. this.ftpConnection.ServerPort = 21;//ftpInfo.FtpPort;
  35. this.ftpConnection.UserName = "autoorder";// ftpInfo.FtpUser;
  36. this.ftpConnection.Password = "86435015";// ftpInfo.FtpPassword;
  37. //this.ftpConnection.DataEncoding = System.Text.Encoding.GetEncoding("UTF-8");
  38. this.ftpConnection.CommandEncoding = System.Text.Encoding.GetEncoding("GBK");
  39. this.ftpConnection.Uploaded += new FTPFileTransferEventHandler(Uploaded_Finished);
  40. }
  41. }
  42. string msg = "上传失败";
  43. int transFileSize = 0;
  44. public string upLoadFile(string type, string path, string servicePath, string JpgPath, string JpgservicePath)
  45. {
  46. ftpConnection.UploadFile(path, servicePath, false);//上传 cdr 或pdf
  47. ftpConnection.UploadFile(JpgPath, JpgservicePath, false);//上传 cdr 或pdf
  48. return msg;
  49. }
  50. //修改名字的方法
  51. #region
  52. private void RenameFile(object sender, FTPFileRenameEventArgs e)
  53. {
  54. //fileName1 原名包括路径
  55. //fileName2 新名字
  56. if (ftpConnection.Exists(fileName1))
  57. fileName1 = GetNewFileName(fileName1);
  58. ftpConnection.RenameFile("原名", fileName1);
  59. }
  60. #endregion
  61. #endregion
  62. void Uploaded_Finished(object objs, FTPFileTransferEventArgs e)
  63. {
  64. msg = "上传成功";
  65. flag = true;
  66. }
  67. //开始上传
  68. public bool uploadFilName(string LocaTionPath, string newFilename)
  69. {
  70. InitFtpConnect();
  71. if (!ftpConnection.IsConnected)
  72. ftpConnection.Connect();
  73. //新文件夹名
  74. this.ftpConnection.UploadFile(LocaTionPath, newFilename);
  75. this.ftpConnection.Close();
  76. return flag;
  77. }
  78. #region 下载文件
  79. public void downFile(string filePath)
  80. {
  81. InitFtpConnect();
  82. if (!ftpConnection.IsConnected)
  83. ftpConnection.Connect();
  84. this.ftpConnection.DownloadByteArray(filePath);
  85. }
  86. #endregion
  87. //防止文件重命名
  88. public static string GetNewFileName(string FileName)
  89. {
  90. Random rand = new Random();
  91. string newfilename = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + "m" +
  92. DateTime.Now.Day.ToString() + "d"
  93. + DateTime.Now.Second.ToString() + DateTime.Now.Minute.ToString()
  94. + DateTime.Now.Millisecond.ToString()
  95. + "a" + rand.Next(1000).ToString()
  96. + FileName.Substring(FileName.LastIndexOf("."), FileName.Length - FileName.LastIndexOf("."));
  97. return newfilename;
  98. }
  99. }