您的位置:首页 > 其它

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

2013-05-13 14:39 288 查看
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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: