关于上传文件的代码问题
private void updata_Click(object sender, EventArgs e){
System.Windows.Forms.OpenFileDialog ofld = new System.Windows.Forms.OpenFileDialog();
if (ofld.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string text = ofld.FileName;
bool fileIsValid = false;
//判断文件否符合要求。
if (this.openFileDialog1.HasFile)
{
//获取上传文件的扩展名
String fileExtension = (this.openFileDialog1.FileName).ToLower();
String[] restrictExtension = { ".gif", ".jpg", "bmp", "png" };
//判断文件类型是否符合要求
for (int i = 0; i < restrictExtension.Length; i++)
{
if (fileExtension == restrictExtension[i])
{
fileIsValid = true;
}
}
//如果文件类型符合要求,?调用SaveAs方法实现上传,并显示相关信息
if (fileIsValid == true) {
try {
this.openFileDialog1.SaveAs(Server.MapPath("~/Picture/") + updataload.FileName);
untCommon.InfoMsg ("文件上传成功|!");
}catch{
untCommon.InfoMsg ( "文件上传失败!");
}
}else{
untCommon.InfoMsg ("只能上传指定扩展名.gif,.jpg,.bmp,png的文件");
}
}
如图,在编程中存在不包含HasFile,SaveAs,Server,FileName的情况