文件上传

<appSettings>
                <!--设置学生基本信息照片保存路径-->
<add key="STUPICTURE" value="~/sp/"/>
<!--设置网上报名照片保存路径-->
<add key="WEBPICTURE" value="~/zp/"/>
<!--设置附件路径-->
<add key="ATTACHPATH" value="~/attach/"/>
</appSettings>
public static readonly string pictureUrl = ConfigurationManager.AppSettings["WEBPICTURE"];  //网上报名照片存放址
public static readonly string StupictureUrl = ConfigurationManager.AppSettings["STUPICTURE"];  //学生基本信息照片存放地址
 
/// <summary>
        /// 上传图片事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSc_Click(object sender, EventArgs e)
        {
           
            if (this.fudSc.HasFile)//检查是否有文件
            {
                string fullFileName = this.fudSc.PostedFile.FileName;                              //文件路径名
                // string fileName = fullFileName.Substring(fullFileName.LastIndexOf("\\") + 1);  //图片名称       
                string type = fullFileName.Substring(fullFileName.LastIndexOf(".") + 1);          //图片格式
                string fileName = this.tbXh.Text.Trim() + "." + type;                                        //以报名号命名图片
                if (type == "jpg" || type == "JPG" )  //判断是否为图片类型
                {
                    if (this.fudSc.PostedFile.ContentLength > 20 * 1024)
                    {
                        Response.Write("<script>alert('上传图片必须小于20k!');</script>");
                    }
                    else
                    {
                        string path = HttpContext.Current.Request.MapPath(pictureUrl);//获取上传文件的网站目录路径
                        if (File.Exists(path + fileName))
                        {
                            this.imgPicture.Src = "";
                            File.Delete(path + fileName);
                        }
                        this.fudSc.SaveAs(path + fileName);//存储文件到磁盘
                        Response.Write("<script>alert('图片上传成功!');</script>");//提示
                        this.imgPicture.Src = "";
                        this.imgPicture.Src = pictureUrl + fileName;//显示图片
                    }

                }
                else
                {
                    Response.Write("<script>alert('非图片类型,不允许上传!');</script>");
                }
            }
            else
            {
                Response.Write("<script>alert('必须指定文件!');</script>");
            }

        }
/// <summary>
        /// 根据学号查询学生信息并进行初始化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void StudentQuery(object sender, EventArgs e)
        {
            StudentEntity studentEntity = studentBll.GetStudentByxh(this.tbXh.Text);
            if (studentEntity.Xh !="")
            {
                this.tbXm.Text = studentEntity.Xm;
                if (studentEntity.Xb == "男")
                    this.rbtnXb.SelectedValue = "男";
                else
                    this.rbtnXb.SelectedValue = "女";
                this.txtSfzh.Text = studentEntity.Sfzh;

                this.imgPicture.Src = "";
                string Webpath = HttpContext.Current.Request.MapPath(pictureUrl);                    //获取网上报名照片上传文件目录路径
                string Stupath = HttpContext.Current.Request.MapPath(StupictureUrl);                //获取学生基本信息照片目录路径
                string OrignFile = "", NewFile = "";
                OrignFile = Stupath + studentEntity.Zp.Trim();                                              //获取学生基本信息照片地址
                NewFile = Webpath + studentEntity.Zp.Trim();                                                    //确定网上报名信息照片地址
                if (File.Exists(NewFile) == true)                                                                  //判断电脑中是否存在照片
                {
                  //存在照片   
                    this.imgPicture.Src = pictureUrl  + studentEntity.Zp.Trim();                      //显示网上报名图片文件夹中的照片
                }
                else if(File .Exists (OrignFile )==true)                                           
                {
                   
                        File.Copy(OrignFile, NewFile, true);                        //拷贝照片
                        this.imgPicture.Src = "";
                        this.imgPicture.Src = pictureUrl + studentEntity.Zp.Trim(); 
                   
                }
             
           
                this.txtCollege.Text = studentEntity.Fy;
                this.txtBh.Text = studentEntity.Bh;
                this.txtXsid.Text = studentEntity.Xsid.ToString();
            }
            else
            {
                Response.Write("<script>alert('没有该学生信息');</script>");
                this.Reset();
            }

        }

原文地址:https://www.cnblogs.com/hubcarl/p/1412729.html