Guest User

Untitled

a guest
Jul 10th, 2018
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.64 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Net;
  6. using System.Web.UI.WebControls;
  7. using System.IO;
  8. using System.Text.RegularExpressions;
  9.  
  10.  
  11. namespace UploadFileOnServer
  12. {
  13. public class UploadFileClass
  14. {
  15. private static string GetValidFileName(string fileName)
  16. {
  17. // remove any invalid character from the filename.
  18. return Regex.Replace(fileName.Trim(), "[^A-Za-z0-9_ ]+", "");
  19. }
  20. public string UploadFile(FileUpload FU, string FolderID)
  21. {
  22.  
  23. string[] validFileTypes = { "bmp", "gif", "png", "jpg", "jpeg", "doc", "docx", "xls", "xlsx", "pdf", "rar", "zip", "ppt" };
  24. string ext = System.IO.Path.GetExtension(FU.PostedFile.FileName).ToLower();
  25. bool isValidFile = false;
  26. for (int i = 0; i < validFileTypes.Length; i++)
  27. {
  28. if (ext == "." + validFileTypes[i])
  29. {
  30. isValidFile = true;
  31. break;
  32. }
  33. }
  34. if (!isValidFile)
  35. {
  36. return "0";
  37.  
  38. }
  39. else
  40. {
  41. string namefile = System.IO.Path.GetFileNameWithoutExtension(FU.FileName);
  42. string file_name = GetValidFileName(namefile);
  43. string remoteFilename = file_name + ext;
  44.  
  45. string ftpUserName = System.Configuration.ConfigurationManager.AppSettings["FTP_UserName"].ToString();
  46. string ftpPassword = System.Configuration.ConfigurationManager.AppSettings["FTP_Password"].ToString();
  47. string FTP_Path = System.Configuration.ConfigurationManager.AppSettings["FTP_Path"].ToString();
  48. string HTTP_Path = System.Configuration.ConfigurationManager.AppSettings["HTTP_Path"].ToString();
  49.  
  50. FtpWebResponse CreateFolderResponse;
  51. try
  52. {
  53.  
  54. //fname = ContentUpload.FileName;
  55. string ftpfullpath = FTP_Path + FolderID + "/";
  56.  
  57. //Create the folder, Please notice if the WPF folder already exist, it will result 550 error.
  58.  
  59. FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);
  60. ftp.Credentials = new NetworkCredential(ftpUserName, ftpPassword);
  61.  
  62. ftp.Method = WebRequestMethods.Ftp.MakeDirectory;
  63. CreateFolderResponse = (FtpWebResponse)ftp.GetResponse();
  64. if (CreateFolderResponse.StatusCode == FtpStatusCode.PathnameCreated)
  65. {
  66. string uri = FTP_Path + FolderID + "/" + remoteFilename;
  67. FtpWebRequest reqFTP;
  68. reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
  69. reqFTP.Credentials = new NetworkCredential(ftpUserName, ftpPassword);
  70. reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory;
  71. reqFTP.KeepAlive = false;
  72. reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
  73. reqFTP.UseBinary = true;
  74. reqFTP.ContentLength = FU.PostedFile.ContentLength;
  75.  
  76. Stream OutputStream = reqFTP.GetRequestStream();
  77.  
  78. int buffLength = 2048000;
  79. byte[] buff = new byte[buffLength];
  80. int contentLen = (int)FU.PostedFile.InputStream.Length;
  81.  
  82. Stream InputStream = FU.PostedFile.InputStream;
  83.  
  84. contentLen = InputStream.Read(buff, 0, buffLength);
  85.  
  86. while (contentLen != 0)
  87. {
  88. OutputStream.Write(buff, 0, contentLen);
  89.  
  90. contentLen = InputStream.Read(buff, 0, buffLength);
  91. }
  92. InputStream.Close();
  93. OutputStream.Close();
  94. }
  95. }
  96. catch
  97. {
  98. try
  99. {
  100. string uri = FTP_Path + FolderID + "/" + remoteFilename;
  101. FtpWebRequest reqFTP;
  102. reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
  103. reqFTP.Credentials = new NetworkCredential(ftpUserName, ftpPassword);
  104. reqFTP.KeepAlive = false;
  105. reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
  106. reqFTP.UseBinary = true;
  107. reqFTP.ContentLength = FU.PostedFile.ContentLength;
  108.  
  109. Stream OutputStream = reqFTP.GetRequestStream();
  110.  
  111. int buffLength = 2048000;
  112. byte[] buff = new byte[buffLength];
  113. int contentLen = (int)FU.PostedFile.InputStream.Length;
  114.  
  115. Stream InputStream = FU.PostedFile.InputStream;
  116.  
  117. contentLen = InputStream.Read(buff, 0, buffLength);
  118.  
  119. while (contentLen != 0)
  120. {
  121. OutputStream.Write(buff, 0, contentLen);
  122.  
  123. contentLen = InputStream.Read(buff, 0, buffLength);
  124. }
  125. InputStream.Close();
  126. OutputStream.Close();
  127. }
  128. catch
  129. {
  130. }
  131. }
  132.  
  133. return remoteFilename;
  134. }
  135.  
  136. }
  137. }
  138.  
  139. }
Add Comment
Please, Sign In to add comment