Advertisement
Guest User

Untitled

a guest
Oct 6th, 2017
81
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. WebClient client = new WebClient();
  157. client.Credentials = new NetworkCredential("username", "password");
  158. client.UploadFile("ftp://ftp.example.com/remote/path/file.zip", @"C:localpathfile.zip");
  159.  
  160. FtpWebRequest request =
  161. (FtpWebRequest)WebRequest.Create("ftp://ftp.example.com/remote/path/file.zip");
  162. request.Credentials = new NetworkCredential("username", "password");
  163. request.Method = WebRequestMethods.Ftp.UploadFile;
  164.  
  165. using (Stream fileStream = File.OpenRead(@"C:localpathfile.zip"))
  166. using (Stream ftpStream = request.GetRequestStream())
  167. {
  168. fileStream.CopyTo(ftpStream);
  169. }
  170.  
  171. FtpWebRequest request =
  172. (FtpWebRequest)WebRequest.Create("ftp://ftp.example.com/remote/path/file.zip");
  173. request.Credentials = new NetworkCredential("username", "password");
  174. request.Method = WebRequestMethods.Ftp.UploadFile;
  175.  
  176. using (Stream fileStream = File.OpenRead(@"C:localpathfile.zip"))
  177. using (Stream ftpStream = request.GetRequestStream())
  178. {
  179. byte[] buffer = new byte[10240];
  180. int read;
  181. int total = 0;
  182. while ((read = fileStream.Read(buffer, 0, buffer.Length)) > 0)
  183. {
  184. ftpStream.Write(buffer, 0, read);
  185. total += read;
  186. Console.WriteLine("Uploaded {0} bytes", total);
  187. }
  188. }
  189.  
  190. private void UploadProfileImage(string TargetFileName, string TargetDestinationPath, string FiletoUpload)
  191. {
  192. //Get the Image Destination path
  193. string imageName = TargetFileName; //you can comment this
  194. string imgPath = TargetDestinationPath;
  195.  
  196. string ftpurl = "ftp://downloads.abc.com/downloads.abc.com/MobileApps/SystemImages/ProfileImages/" + imgPath;
  197. string ftpusername = krayknot_DAL.clsGlobal.FTPUsername;
  198. string ftppassword = krayknot_DAL.clsGlobal.FTPPassword;
  199. string fileurl = FiletoUpload;
  200.  
  201. FtpWebRequest ftpClient = (FtpWebRequest)FtpWebRequest.Create(ftpurl);
  202. ftpClient.Credentials = new System.Net.NetworkCredential(ftpusername, ftppassword);
  203. ftpClient.Method = System.Net.WebRequestMethods.Ftp.UploadFile;
  204. ftpClient.UseBinary = true;
  205. ftpClient.KeepAlive = true;
  206. System.IO.FileInfo fi = new System.IO.FileInfo(fileurl);
  207. ftpClient.ContentLength = fi.Length;
  208. byte[] buffer = new byte[4097];
  209. int bytes = 0;
  210. int total_bytes = (int)fi.Length;
  211. System.IO.FileStream fs = fi.OpenRead();
  212. System.IO.Stream rs = ftpClient.GetRequestStream();
  213. while (total_bytes > 0)
  214. {
  215. bytes = fs.Read(buffer, 0, buffer.Length);
  216. rs.Write(buffer, 0, bytes);
  217. total_bytes = total_bytes - bytes;
  218. }
  219. //fs.Flush();
  220. fs.Close();
  221. rs.Close();
  222. FtpWebResponse uploadResponse = (FtpWebResponse)ftpClient.GetResponse();
  223. string value = uploadResponse.StatusDescription;
  224. uploadResponse.Close();
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement