请选择 进入手机版 | 继续访问电脑版
查看: 697|回复: 0

[ASP.NET教程] C# linux FTP client [客户端]

3万

主题

3万

帖子

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
100197
发表于 2015-11-23 18:11:17
<无详细内容>
  1. /// <summary>
  2. /// 图片浏览
  3. /// </summary>
  4. /// <param name="sender"></param>
  5. /// <param name="e"></param>
  6. private void button3_Click(object sender, EventArgs e)
  7. {
  8. OpenFileDialog ofd = new OpenFileDialog();
  9. ofd.Title = "打开(Open)";
  10. ofd.FileName = "";
  11. ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);//为了获取特定的系统文件夹,可以使用System.Environment类的静态方法GetFolderPath()。该方法接受一个 Environment.SpecialFolder枚举,其中可以定义要返回路径的哪个系统目录
  12. ofd.Filter = "CSV文件(*.jpg)|*.jpg";
  13. ofd.ValidateNames = true; //文件有效性验证ValidateNames,验证用户输入是否是一个有效的Windows文件名
  14. ofd.CheckFileExists = true; //验证路径有效性
  15. ofd.CheckPathExists = true; //验证文件有效性
  16. if (ofd.ShowDialog() == DialogResult.OK)
  17. {
  18. filepath = ofd.FileName.Substring(0, ofd.FileName.Length - ofd.SafeFileName.Length - 1);
  19. filepath = filepath.Replace("/", @"\");
  20. filename = ofd.SafeFileName;
  21. textBox4.Text = filepath + @"" + filename;
  22. }
  23. }
  24. /// <summary>
  25. /// FTP 上传
  26. /// </summary>
  27. /// <param name="sender"></param>
  28. /// <param name="e"></param>
  29. private void button2_Click_1(object sender, EventArgs e)
  30. {
  31. file_name_url_image_d = "mili" + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + ".jpg";
  32. FtpStatusCode status = UploadFun(textBox4.Text, "ftp://203.86.26.190/milimall/images_d/images/upload/" + file_name_url_image_d);
  33. FtpStatusCode status2 = UploadFun(textBox4.Text, "ftp://203.86.26.190/milimall/images/upload/Image/" + file_name_url_image_d);
  34. if (status.ToString() == "ClosingData" && status2.ToString() == "ClosingData")
  35. {
  36. MessageBox.Show("上传成功");
  37. MessageBox.Show("ftp://203.86.26.190/milimall/images_d/images/upload/Image/" + file_name_url_image_d);
  38. mark_upload = true;
  39. }
  40. else
  41. {
  42. file_name_url_image_d = "";
  43. MessageBox.Show("上传失败");
  44. }
  45. }
  46. #region FTP 上传类
  47. /// <summary>
  48. /// FTP 上传类
  49. /// </summary>
  50. /// <param name="fileName"></param>
  51. /// <param name="uploadUrl"></param>
  52. /// <returns></returns>
  53. private FtpStatusCode UploadFun(string fileName, string uploadUrl)
  54. {
  55. Stream requestStream = null;
  56. FileStream fileStream = null;
  57. FtpWebResponse uploadResponse = null;
  58. try
  59. {
  60. FtpWebRequest uploadRequest =
  61. (FtpWebRequest)WebRequest.Create(uploadUrl);
  62. uploadRequest.Method = WebRequestMethods.Ftp.UploadFile;
  63. uploadRequest.Proxy = null;
  64. NetworkCredential nc = new NetworkCredential();
  65. //nc.Domain = "203.86.26.190";
  66. nc.UserName = "milimall";
  67. nc.Password = "wesdxc91";
  68. // uploadRequest.UseBinary = true;
  69. uploadRequest.Credentials = nc; //修改getCredential();错误2
  70. requestStream = uploadRequest.GetRequestStream();
  71. fileStream = File.Open(fileName, FileMode.Open);
  72. byte[] buffer = new byte[1024];
  73. int bytesRead;
  74. while (true)
  75. {
  76. bytesRead = fileStream.Read(buffer, 0, buffer.Length);
  77. if (bytesRead == 0)
  78. break;
  79. requestStream.Write(buffer, 0, bytesRead);
  80. }
  81. requestStream.Close();
  82. uploadResponse = (FtpWebResponse)uploadRequest.GetResponse();
  83. return uploadResponse.StatusCode;
  84. }
  85. catch (UriFormatException ex)
  86. {
  87. }
  88. catch (IOException ex)
  89. {
  90. }
  91. catch (WebException ex)
  92. {
  93. }
  94. finally
  95. {
  96. if (uploadResponse != null)
  97. uploadResponse.Close();
  98. if (fileStream != null)
  99. fileStream.Close();
  100. if (requestStream != null)
  101. requestStream.Close();
  102. }
  103. return FtpStatusCode.Undefined;
  104. }
  105. #endregion
复制代码


回复

使用道具 举报