Advertisement
Guest User

Untitled

a guest
Aug 16th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.64 KB | None | 0 0
  1. private void button2_Click(object sender, EventArgs e)
  2. {
  3. var dirPath = @"C:/Documents and Settings/sander.GD/Bureaublad/test/";
  4.  
  5. ftp ftpClient = new ftp("ftp://example.com/", "username", "password");
  6.  
  7. string[] files = Directory.GetFiles(dirPath,"*.*");
  8.  
  9. var uploadPath = "/httpdocs/album";
  10.  
  11. foreach (string file in files)
  12. {
  13. ftpClient.createDirectory("/test");
  14.  
  15. ftpClient.upload(uploadPath + "/" + Path.GetFileName(file), file);
  16. }
  17.  
  18. if (string.IsNullOrEmpty(txtnaam.Text))
  19. {
  20. MessageBox.Show("Gelieve uw naam in te geven !");
  21. }
  22. }
  23.  
  24. using (WebClient client = new WebClient())
  25. {
  26. client.Credentials = new NetworkCredential(ftpUsername, ftpPassword);
  27. client.UploadFile("ftp://ftpserver.com/target.zip", "STOR", localFilePath);
  28. }
  29.  
  30. public void UploadFtpFile(string folderName, string fileName)
  31. {
  32.  
  33. FtpWebRequest request;
  34. try
  35. {
  36. string folderName;
  37. string fileName;
  38. string absoluteFileName = Path.GetFileName(fileName);
  39.  
  40. request = WebRequest.Create(new Uri(string.Format(@"ftp://{0}/{1}/{2}", "127.0.0.1", folderName, absoluteFileName))) as FtpWebRequest;
  41. request.Method = WebRequestMethods.Ftp.UploadFile;
  42. request.UseBinary = 1;
  43. request.UsePassive = 1;
  44. request.KeepAlive = 1;
  45. request.Credentials = new NetworkCredential(user, pass);
  46. request.ConnectionGroupName = "group";
  47.  
  48. using (FileStream fs = File.OpenRead(fileName))
  49. {
  50. byte[] buffer = new byte[fs.Length];
  51. fs.Read(buffer, 0, buffer.Length);
  52. fs.Close();
  53. Stream requestStream = request.GetRequestStream();
  54. requestStream.Write(buffer, 0, buffer.Length);
  55. requestStream.Flush();
  56. requestStream.Close();
  57. }
  58. }
  59. catch (Exception ex)
  60. {
  61.  
  62. }
  63. }
  64.  
  65. UploadFtpFile("testFolder", "E:\filesToUpload\test.img");
  66.  
  67. request = WebRequest.Create(new Uri(string.Format(@"ftp://{0}/{1}/", "127.0.0.1", "testFolder"))) as FtpWebRequest;
  68. request.Method = WebRequestMethods.Ftp.MakeDirectory;
  69. FtpWebResponse ftpResponse = (FtpWebResponse)request.GetResponse();
  70.  
  71. public virtual void Send(string fileName, byte[] file)
  72. {
  73. ByteArrayToFile(fileName, file);
  74.  
  75. var request = (FtpWebRequest) WebRequest.Create(new Uri(ServerUrl + fileName));
  76.  
  77. request.Method = WebRequestMethods.Ftp.UploadFile;
  78. request.UsePassive = false;
  79. request.Credentials = new NetworkCredential(UserName, Password);
  80. request.ContentLength = file.Length;
  81.  
  82. var requestStream = request.GetRequestStream();
  83. requestStream.Write(file, 0, file.Length);
  84. requestStream.Close();
  85.  
  86. var response = (FtpWebResponse) request.GetResponse();
  87.  
  88. if (response != null)
  89. response.Close();
  90. }
  91.  
  92. byte[] bytes = File.ReadAllBytes(dir + file);
  93.  
  94. public static void UploadFileToFtp(string url, string filePath, string username, string password)
  95. {
  96. var fileName = Path.GetFileName(filePath);
  97. var request = (FtpWebRequest)WebRequest.Create(url + fileName);
  98.  
  99. request.Method = WebRequestMethods.Ftp.UploadFile;
  100. request.Credentials = new NetworkCredential(username, password);
  101. request.UsePassive = true;
  102. request.UseBinary = true;
  103. request.KeepAlive = false;
  104.  
  105. using (var fileStream = File.OpenRead(filePath))
  106. {
  107. using (var requestStream = request.GetRequestStream())
  108. {
  109. fileStream.CopyTo(requestStream);
  110. requestStream.Close();
  111. }
  112. }
  113.  
  114. var response = (FtpWebResponse)request.GetResponse();
  115. Console.WriteLine("Upload done: {0}", response.StatusDescription);
  116. response.Close();
  117. }
  118.  
  119. requestStream.Flush();
  120. requestStream.Close();
  121.  
  122. //Secure FTP
  123. public void SecureFTPUploadFile(string destinationHost,int port,string username,string password,string source,string destination)
  124.  
  125. {
  126. ConnectionInfo ConnNfo = new ConnectionInfo(destinationHost, port, username, new PasswordAuthenticationMethod(username, password));
  127.  
  128. var temp = destination.Split('/');
  129. string destinationFileName = temp[temp.Count() - 1];
  130. string parentDirectory = destination.Remove(destination.Length - (destinationFileName.Length + 1), destinationFileName.Length + 1);
  131.  
  132.  
  133. using (var sshclient = new SshClient(ConnNfo))
  134. {
  135. sshclient.Connect();
  136. using (var cmd = sshclient.CreateCommand("mkdir -p " + parentDirectory + " && chmod +rw " + parentDirectory))
  137. {
  138. cmd.Execute();
  139. }
  140. sshclient.Disconnect();
  141. }
  142.  
  143.  
  144. using (var sftp = new SftpClient(ConnNfo))
  145. {
  146. sftp.Connect();
  147. sftp.ChangeDirectory(parentDirectory);
  148. using (var uplfileStream = System.IO.File.OpenRead(source))
  149. {
  150. sftp.UploadFile(uplfileStream, destinationFileName, true);
  151. }
  152. sftp.Disconnect();
  153. }
  154. }
  155.  
  156. private void UploadProfileImage(string TargetFileName, string TargetDestinationPath, string FiletoUpload)
  157. {
  158. //Get the Image Destination path
  159. string imageName = TargetFileName; //you can comment this
  160. string imgPath = TargetDestinationPath;
  161.  
  162. string ftpurl = "ftp://downloads.abc.com/downloads.abc.com/MobileApps/SystemImages/ProfileImages/" + imgPath;
  163. string ftpusername = krayknot_DAL.clsGlobal.FTPUsername;
  164. string ftppassword = krayknot_DAL.clsGlobal.FTPPassword;
  165. string fileurl = FiletoUpload;
  166.  
  167. FtpWebRequest ftpClient = (FtpWebRequest)FtpWebRequest.Create(ftpurl);
  168. ftpClient.Credentials = new System.Net.NetworkCredential(ftpusername, ftppassword);
  169. ftpClient.Method = System.Net.WebRequestMethods.Ftp.UploadFile;
  170. ftpClient.UseBinary = true;
  171. ftpClient.KeepAlive = true;
  172. System.IO.FileInfo fi = new System.IO.FileInfo(fileurl);
  173. ftpClient.ContentLength = fi.Length;
  174. byte[] buffer = new byte[4097];
  175. int bytes = 0;
  176. int total_bytes = (int)fi.Length;
  177. System.IO.FileStream fs = fi.OpenRead();
  178. System.IO.Stream rs = ftpClient.GetRequestStream();
  179. while (total_bytes > 0)
  180. {
  181. bytes = fs.Read(buffer, 0, buffer.Length);
  182. rs.Write(buffer, 0, bytes);
  183. total_bytes = total_bytes - bytes;
  184. }
  185. //fs.Flush();
  186. fs.Close();
  187. rs.Close();
  188. FtpWebResponse uploadResponse = (FtpWebResponse)ftpClient.GetResponse();
  189. string value = uploadResponse.StatusDescription;
  190. uploadResponse.Close();
  191. }
  192.  
  193. WebClient client = new WebClient();
  194. client.Credentials = new NetworkCredential("username", "password");
  195. client.UploadFile("ftp://ftp.example.com/remote/path/file.zip", @"C:localpathfile.zip");
  196.  
  197. FtpWebRequest request =
  198. (FtpWebRequest)WebRequest.Create("ftp://ftp.example.com/remote/path/file.zip");
  199. request.Credentials = new NetworkCredential("username", "password");
  200. request.Method = WebRequestMethods.Ftp.UploadFile;
  201.  
  202. using (Stream fileStream = File.OpenRead(@"C:localpathfile.zip"))
  203. using (Stream ftpStream = request.GetRequestStream())
  204. {
  205. fileStream.CopyTo(ftpStream);
  206. }
  207.  
  208. FtpWebRequest request =
  209. (FtpWebRequest)WebRequest.Create("ftp://ftp.example.com/remote/path/file.zip");
  210. request.Credentials = new NetworkCredential("username", "password");
  211. request.Method = WebRequestMethods.Ftp.UploadFile;
  212.  
  213. using (Stream fileStream = File.OpenRead(@"C:localpathfile.zip"))
  214. using (Stream ftpStream = request.GetRequestStream())
  215. {
  216. byte[] buffer = new byte[10240];
  217. int read;
  218. int total = 0;
  219. while ((read = fileStream.Read(buffer, 0, buffer.Length)) > 0)
  220. {
  221. ftpStream.Write(buffer, 0, read);
  222. total += read;
  223. Console.WriteLine("Uploaded {0} bytes", total);
  224. }
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement