标题:关于上传文件的代码问题
取消只看楼主
李俊钊
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2014-6-12
 问题点数:0 回复次数:0 
关于上传文件的代码问题
  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的情况
搜索更多相关主题的帖子: private 上传文件 扩展名 false 
2014-06-12 17:16



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-432845-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.232267 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved