Guest User

Untitled

a guest
Jan 17th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.31 KB | None | 0 0
  1. private string Upload(string ftpServer, string userName, string password, string filename)
  2. {
  3. string reply = "Success";
  4. try
  5. {
  6. using (System.Net.WebClient client = new System.Net.WebClient()) //System.Net.WebClient client = new System.Net.WebClient()
  7. {
  8. client.Credentials = new System.Net.NetworkCredential(userName, password);
  9. client.Proxy = new WebProxy();
  10. FileInfo fi = new FileInfo(filename);
  11. client.UploadFile(ftpServer + "//" + fi.Name, "STOR", filename);
  12. }
  13. }
  14. catch (Exception ex)
  15. {
  16. reply = ex.Message;
  17. }
  18. return reply;
  19. }
  20.  
  21. FtpWebRequest reqFTP;
  22. // Create FtpWebRequest object from the Uri provided
  23. reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
  24. reqFTP.Proxy = null;
  25. // Provide the WebPermission Credintials
  26. reqFTP.Credentials = new NetworkCredential(user, pass);
  27.  
  28. // By default KeepAlive is true, where the control connection is not closed
  29. // after a command is executed.
  30. reqFTP.KeepAlive = false;
  31.  
  32. // Specify the command to be executed.
  33. reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
  34.  
  35. // Specify the data transfer type.
  36. reqFTP.UseBinary = true;
  37.  
  38. // Notify the server about the size of the uploaded file
  39. FileStream fs = File.OpenRead(filename);
  40. reqFTP.ContentLength = fileInf.Length;
  41. // The buffer size is set to 2kb
  42. int buffLength = Convert.ToInt32(fs.Length);
  43. byte[] buff = new byte[buffLength];
  44. int contentLen;
  45.  
  46. try
  47. {
  48. // Stream to which the file to be upload is written
  49. Stream strm = reqFTP.GetRequestStream();
  50.  
  51. // Read from the file stream 2kb at a time
  52. contentLen = fs.Read(buff, 0, buffLength);
  53.  
  54. // Till Stream content ends
  55. while (contentLen != 0)
  56. {
  57. // Write Content from the file stream to the FTP Upload Stream
  58. strm.Write(buff, 0, contentLen);
  59. contentLen = fs.Read(buff, 0, buffLength);
  60. }
  61.  
  62. // Close the file stream and the Request Stream
  63. strm.Close();
  64. fs.Close();
  65. }
  66.  
  67. catch (Exception ex)
  68. {
  69. string s = ex.Message;
  70. }
  71.  
  72. }
  73.  
  74. public static void Sample(string filename)
  75. {
  76.  
  77. // Get the object used to communicate with the server.
  78. FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://serverip/"); //test.htm
  79. request.Method = WebRequestMethods.Ftp.UploadFile;
  80.  
  81. // This example assumes the FTP site uses anonymous logon.
  82. request.Credentials = new NetworkCredential (user,passs);
  83.  
  84. try
  85. {
  86. // Copy the contents of the file to the request stream.
  87. StreamReader sourceStream = new StreamReader(filename);
  88. byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
  89. sourceStream.Close();
  90. request.ContentLength = fileContents.Length;
  91.  
  92. request.Proxy = null;
  93.  
  94. Stream requestStream = request.GetRequestStream();
  95. requestStream.Write(fileContents, 0, fileContents.Length);
  96. requestStream.Close();
  97.  
  98. FtpWebResponse response = (FtpWebResponse)request.GetResponse();
  99.  
  100. Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);
  101.  
  102. response.Close();
  103. }
  104. catch (Exception ex)
  105. {
  106. Console.WriteLine(ex.Message);
  107. }
  108.  
  109. Console.ReadLine();
  110.  
  111. }
  112.  
  113. bool result = false;
  114. long length = 0;
  115. // Set up the FTP upload.
  116. // The URI for the request specifies the protocol, the server and the filename.
  117. FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create("ftp://" + ftpServerUrl + "/" + targetFilename);
  118. ftpRequest.EnableSsl = false;
  119. ftpRequest.KeepAlive = true;
  120. ftpRequest.ReadWriteTimeout = ftpTimeout; // To perform an individual read or write.
  121. ftpRequest.Timeout = ftpTimeout; // To establish a connection or start an operation.
  122. ftpRequest.Credentials = new NetworkCredential(ftpUsername, ftpPassword);
  123. ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
  124. ftpRequest.UseBinary = true;
  125. ftpRequest.UsePassive = true;
  126.  
  127. // Upload the file.
  128. using (FileStream fileStream = File.OpenRead(filename))
  129. {
  130. using (Stream ftpStream = ftpRequest.GetRequestStream())
  131. {
  132. fileStream.CopyTo(ftpStream);
  133. length = fileStream.Length;
  134. ftpStream.Close();
  135. }
  136. FtpWebResponse ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
  137. if (ftpResponse.StatusCode == FtpStatusCode.ClosingData)
  138. result = true;
  139. else
  140. throw new Exception(ftpResponse.StatusDescription + " (" + ftpResponse.StatusCode + ")");
  141. ftpResponse.Close();
  142. }
Add Comment
Please, Sign In to add comment