- /// <summary>
- /// 图片浏览
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void button3_Click(object sender, EventArgs e)
- {
- OpenFileDialog ofd = new OpenFileDialog();
- ofd.Title = "打开(Open)";
- ofd.FileName = "";
- ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);//为了获取特定的系统文件夹,可以使用System.Environment类的静态方法GetFolderPath()。该方法接受一个 Environment.SpecialFolder枚举,其中可以定义要返回路径的哪个系统目录
- ofd.Filter = "CSV文件(*.jpg)|*.jpg";
- ofd.ValidateNames = true; //文件有效性验证ValidateNames,验证用户输入是否是一个有效的Windows文件名
- ofd.CheckFileExists = true; //验证路径有效性
- ofd.CheckPathExists = true; //验证文件有效性
- if (ofd.ShowDialog() == DialogResult.OK)
- {
- filepath = ofd.FileName.Substring(0, ofd.FileName.Length - ofd.SafeFileName.Length - 1);
- filepath = filepath.Replace("/", @"\\\\");
- filename = ofd.SafeFileName;
- textBox4.Text = filepath + @"\\" + filename;
- }
- }
- /// <summary>
- /// FTP 上传
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void button2_Click_1(object sender, EventArgs e)
- {
- 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";
- FtpStatusCode status = UploadFun(textBox4.Text, "ftp://203.86.26.190/milimall/images_d/images/upload/" + file_name_url_image_d);
- FtpStatusCode status2 = UploadFun(textBox4.Text, "ftp://203.86.26.190/milimall/images/upload/Image/" + file_name_url_image_d);
- if (status.ToString() == "ClosingData" && status2.ToString() == "ClosingData")
- {
- MessageBox.Show("上传成功");
- MessageBox.Show("ftp://203.86.26.190/milimall/images_d/images/upload/Image/" + file_name_url_image_d);
- mark_upload = true;
- }
- else
- {
- file_name_url_image_d = "";
- MessageBox.Show("上传失败");
- }
- }
- #region FTP 上传类
- /// <summary>
- /// FTP 上传类
- /// </summary>
- /// <param name="fileName"></param>
- /// <param name="uploadUrl"></param>
- /// <returns></returns>
- private FtpStatusCode UploadFun(string fileName, string uploadUrl)
- {
- Stream requestStream = null;
- FileStream fileStream = null;
- FtpwebResponse uploadResponse = null;
- try
- {
- FtpWebRequest uploadRequest =
- (FtpWebRequest)WebRequest.Create(uploadUrl);
- uploadRequest.Method = WebRequestMethods.Ftp.UploadFile;
- uploadRequest.Proxy = null;
- NetworkCredential nc = new NetworkCredential();
- //nc.Domain = "203.86.26.190";
- nc.UserName = "milimall";
- nc.Password = "wesdxc91";
- // uploadRequest.UseBinary = true;
- uploadRequest.Credentials = nc; //修改getCredential();错误2
- requestStream = uploadRequest.GetRequestStream();
- fileStream = File.Open(fileName, FileMode.Open);
- byte[] buffer = new byte[1024];
- int bytesRead;
- while (true)
- {
- bytesRead = fileStream.Read(buffer, 0, buffer.Length);
- if (bytesRead == 0)
- break;
- requestStream.Write(buffer, 0, bytesRead);
- }
- requestStream.Close();
- uploadResponse = (FtpWebResponse)uploadRequest.GetResponse();
- return uploadResponse.StatusCode;
- }
- catch (UriFormatException ex)
- {
- }
- catch (IOException ex)
- {
- }
- catch (WebException ex)
- {
- }
- finally
- {
- if (uploadResponse != null)
- uploadResponse.Close();
- if (fileStream != null)
- fileStream.Close();
- if (requestStream != null)
- requestStream.Close();
- }
- return FtpStatusCode.Undefined;
- }
- #endregion
- //该片段来自于http://www.codesnippet.cn/detail/2003201511935.html
来源: http://www.codesnippet.cn/detail/2003201511935.html