zoukankan      html  css  js  c++  java
  • DetailsView结合fileupload的使用

    protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
        {
            string strPathSave;
            string strFileName;

            FileUpload f = (FileUpload)DetailsView1.FindControl("FileUpload1");

            //FileUpload1控件是否有上传的文件
            if (f.HasFile)
            {
                //取得文件上传后的完整路径和新的文件名称
                strFileName = f.FileName;
                strPathSave = Server.MapPath("/Images/StorePic/");
                strPathSave = strPathSave + System.DateTime.Now.ToString("yyyyMMddhhmmss") + strFileName.Substring(strFileName.LastIndexOf("."));

                //PostedFile对象也具有一个FileName属性,但是表示的是上传文件的全路径名,需要手工提取文件名
                f.SaveAs(strPathSave);

                //手工绑定,指定参数的默认值
                this.SqlDataSource1.InsertParameters["STOREPIC"].DefaultValue = strFileName;

                ////文件夹不存在的时候,创建文件夹
                //if (!System.IO.Directory.Exists(Server.MapPath(strUpPath)))
                //{
                //     System.IO.Directory.CreateDirectory(Server.MapPath(strUpPath));
                //}
            }
        }

        protected void DetailsView1_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
        {
            string strPathSave;
            string strFileName;

            FileUpload f = (FileUpload)DetailsView1.FindControl("FileUpload1");

            //FileUpload1控件是否有上传的文件
            if (f.HasFile)
            {
                //取得文件上传后的完整路径和新的文件名称
                strFileName = f.FileName;
                strPathSave = Server.MapPath("/Images/StorePic/");
                strPathSave = strPathSave + System.DateTime.Now.ToString("yyyyMMddhhmmss") + strFileName.Substring(strFileName.LastIndexOf("."));

                //PostedFile对象也具有一个FileName属性,但是表示的是上传文件的全路径名,需要手工提取文件名
                f.SaveAs(strPathSave);

                //手工绑定,指定参数的默认值
                this.SqlDataSource1.InsertParameters["STOREPIC"].DefaultValue = strPathSave;

                ////文件夹不存在的时候,创建文件夹
                //if (!System.IO.Directory.Exists(Server.MapPath(strUpPath)))
                //{
                //     System.IO.Directory.CreateDirectory(Server.MapPath(strUpPath));
                //}
            }
        }

  • 相关阅读:
    python day09
    python day08
    python day07
    python day06
    python day05
    Django-Form组件之字段
    python装饰器
    npm常用命令
    python常用模块-re 正则表达式
    Django-forms效验组件
  • 原文地址:https://www.cnblogs.com/cosiray/p/1551969.html
Copyright © 2011-2022 走看看