winform中如何将本地图片上传到服务器

时间:2022-09-08 12:12:34

pictureBox1.BackgroundImage = null;
            OpenFileDialog ofDlg = new OpenFileDialog();
            DialogResult dRet = ofDlg.ShowDialog();
            if (dRet == DialogResult.OK)
            {
                string strFile = ofDlg.FileName;
                string type = strFile.Substring(strFile.LastIndexOf(".") + 1);
                if (type == "bmp" || type == "jpg" || type == "gif" || type == "jpeg" || type == "png")
                {
                    txtSingle.Text = strFile;
                    pictureBox1.ImageLocation = txtSingle.Text;
                    System.IO.Directory.SetCurrentDirectory(Application.StartupPath);
                    lblShow.Hide();
                }
                else
                {
                    MessageBox.Show("不是有效的图片格式!");
                }
            }
            else
            {
                txtSingle.Text = "";
            }

上传代码:

 string filepath = this.txtSingle.Text.Trim();
            if (filepath == string.Empty)
            {
                MessageBox.Show("图片路径不能为空!");
                return;
            }
            FileInfo info = new FileInfo(filepath);
            string imgpath = "\\images\\";
            //string dirpath = Application.StartupPath + imgpath;

        string dirpath = \\\\ip\\路径\\+ imgpath
            try
            {
                if (!Directory.Exists(dirpath))
                {
                    Directory.CreateDirectory(dirpath);
                }
                info.CopyTo(dirpath + info.Name, true);
                txtSingle.Text = imgpath + info.Name;
                MessageBox.Show("图片上传成功!");


            }
            catch (Exception ex)
            {
                lblShow.Show();
                this.lblShow.Text = "图片上传失败,失败原因:" + ex.Message;
            }