Advertisement
Guest User

Untitled

a guest
Sep 15th, 2017
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. Public void upload ()
  2. {
  3.  
  4. String sourcefilepath ="C:\Users\TST\Desktop\TESTOWS.txt";
  5. String ftpurl = "1.1.1.1/LIBRARY";
  6. String ftpusername = "ACC";
  7. String ftppassword = "ACC";
  8. Stream requestStream = null;
  9.  
  10. FileStream fileStream = null;
  11. FtpWebResponse uploadResponse = null;
  12.  
  13. try
  14. {
  15. string filename = Path.GetFileName(sourcefilepath);
  16. string ftpfullpath = @"ftp://" + ftpurl + "/" + filename;
  17. FtpWebRequest uploadRequest = (FtpWebRequest)WebRequest.Create(string.Format(ftpfullpath));
  18. uploadRequest.Method = WebRequestMethods.Ftp.UploadFile;
  19.  
  20. //Since the FTP you are downloading to is secure, send
  21. //in user name and password to be able upload the file
  22.  
  23. ICredentials credentials = new NetworkCredential(ftpusername, ftppassword);
  24.  
  25. uploadRequest.Credentials = credentials;
  26. uploadRequest.UsePassive = true;
  27. uploadRequest.UseBinary = false;
  28. uploadRequest.KeepAlive = false;
  29.  
  30. //UploadFile is not supported through an Http proxy
  31. //so we disable the proxy for this request.
  32.  
  33. uploadRequest.Proxy = null;
  34. //uploadRequest.UsePassive = false; <--found from another forum and did not make a difference
  35.  
  36. requestStream = uploadRequest.GetRequestStream();
  37. fileStream = File.Open(sourcefilepath, FileMode.Open);
  38.  
  39. byte[] buffer = new byte[1024];
  40. int bytesRead;
  41.  
  42. while (true)
  43. {
  44. bytesRead = fileStream.Read(buffer, 0, buffer.Length);
  45. if (bytesRead == 0)
  46. break;
  47.  
  48. requestStream.Write(buffer, 0, bytesRead);
  49. }
  50.  
  51. //The request stream must be closed before getting
  52. //the response.
  53.  
  54. requestStream.Close();
  55. uploadResponse = (FtpWebResponse)uploadRequest.GetResponse();
  56. }
  57. catch (UriFormatException ex)
  58. {
  59. MessageBox.Show(ex.Message);
  60. }
  61. catch (IOException ex)
  62. {
  63. MessageBox.Show(ex.Message);
  64. }
  65. catch (WebException ex)
  66. {
  67. MessageBox.Show(ex.Message);
  68. }
  69. finally
  70. {
  71. if (uploadResponse != null)
  72. uploadResponse.Close();
  73.  
  74. if (fileStream != null)
  75. fileStream.Close();
  76.  
  77. if (requestStream != null)
  78. requestStream.Close();
  79. }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement