UpLoad.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. using System;
  2. using System.Web;
  3. using System.Web.UI.WebControls;
  4. using System.Web.UI.HtmlControls;
  5. using System.IO;
  6. namespace Bowin.Common
  7. {
  8. /// <summary>
  9. /// UpLoad 的摘要说明。
  10. /// </summary>
  11. public class UpLoad
  12. {
  13. public UpLoad()
  14. {
  15. //
  16. // TODO: 在此处添加构造函数逻辑
  17. //
  18. }
  19. public static bool IsFileExist=false;
  20. /// <summary>
  21. /// 文件上传
  22. /// </summary>
  23. ///
  24. public string UpLoadFile(byte[] buffer ,string sFileName )
  25. {
  26. string filePath = System.Configuration.ConfigurationManager.AppSettings["__ImportDataFileUrl__"];
  27. int iCounter = 0;
  28. string downUrl = "";
  29. FileManager fileManager = new FileManager();
  30. string sEqualFileName = sFileName;
  31. while ( true )
  32. {
  33. if (fileManager.IsFileExist (sEqualFileName))
  34. {
  35. iCounter++ ;
  36. sEqualFileName = System.IO.Path.GetFileNameWithoutExtension(sFileName) + "(" + iCounter.ToString() + ")" +
  37. System.IO.Path.GetExtension(sFileName);
  38. }
  39. else
  40. {
  41. sFileName = sEqualFileName;
  42. break ;
  43. }
  44. }
  45. if (!sFileName.Equals(""))
  46. {
  47. if (UpLoadToFileServer(buffer, sFileName))
  48. {
  49. downUrl = filePath + sFileName;
  50. }
  51. }
  52. return downUrl;
  53. }
  54. //public string UpLoadFile(byte[] buffer, string sFileName)
  55. //{
  56. // string sFilePwd = "BBEFC0B4C3C5FE75391ACD4984635C6E";
  57. // FileHandle.FileHandle upload = new DGDP.Bowin.Business.FileHandle.FileHandle();
  58. // FileHandle.ResultOfString result = upload.Upload(sFilePwd, buffer, "/FSGIS/", sFileName);
  59. // if (result.Code == 1)
  60. // {
  61. // return result.Detail;
  62. // }
  63. // else
  64. // {
  65. // return "";
  66. // }
  67. //}
  68. /// <summary>
  69. /// 文件上传
  70. /// </summary>
  71. public string UpLoadFile(HttpPostedFile fileControl , string sFileName)
  72. {
  73. string filePath = System.Configuration.ConfigurationManager.AppSettings["__ImportDataFileUrl__"];
  74. int iCounter = 0;
  75. string downUrl = "";
  76. FileManager fileManager = new FileManager();
  77. string sEqualFileName = sFileName;
  78. while (true)
  79. {
  80. if (fileManager.IsFileExist(sEqualFileName))
  81. {
  82. iCounter++;
  83. sEqualFileName = System.IO.Path.GetFileNameWithoutExtension(sFileName) + "(" + iCounter.ToString() + ")" +
  84. System.IO.Path.GetExtension(sFileName);
  85. }
  86. else
  87. {
  88. sFileName = sEqualFileName;
  89. break;
  90. }
  91. }
  92. if (!sFileName.Equals(""))
  93. {
  94. if (UpLoadToFileServer(fileControl, sFileName))
  95. {
  96. downUrl = filePath + sFileName;
  97. }
  98. }
  99. return downUrl;
  100. }
  101. /// <summary>
  102. /// 上传到文件服务器
  103. /// update by :zqz 20090614
  104. /// </summary>
  105. /// <param name="fileControl">上传控件</param>
  106. /// <param name="filePath">上传路径</param>
  107. /// <param name="fileName">文件名</param>
  108. /// <returns>bool值</returns>
  109. private bool UpLoadToFileServer(HttpPostedFile fileControl, string fileName)
  110. {
  111. try
  112. {
  113. /*-----需要在web.config的AppSetting节点添加以下三个节点
  114. <add key="FileServerPath" value="您的文件服务器地址"/>
  115. <add key="FileServerUser" value="您的文件服务器名称" />
  116. <add key="FileServerPass" value="您的文件服务器密码" />*/
  117. FileManager fileManager = new FileManager();
  118. fileManager.UploadFile(fileControl.InputStream, fileName);
  119. }
  120. catch
  121. {
  122. return false;
  123. }
  124. return true;
  125. }
  126. /// <summary>
  127. /// 上传到文件服务器
  128. /// </summary>
  129. /// <param name="fileControl">上传控件</param>
  130. /// <param name="filePath">上传路径</param>
  131. /// <param name="fileName">文件名</param>
  132. /// <returns>bool值</returns>
  133. private bool UpLoadToFileServer(byte[] buffer,string fileName)
  134. {
  135. try
  136. {
  137. /*-----需要在web.config的AppSetting节点添加以下三个节点
  138. <add key="FileServerPath" value="您的文件服务器地址"/>
  139. <add key="FileServerUser" value="您的文件服务器名称" />
  140. <add key="FileServerPass" value="您的文件服务器密码" />*/
  141. FileManager fileManager = new FileManager ();
  142. return fileManager.UploadFile(buffer,fileName);
  143. }
  144. catch
  145. {
  146. return false;
  147. }
  148. }
  149. }
  150. }