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));
            //}
        }
    }

相关文章:

  • 2022-12-23
  • 2022-01-26
  • 2022-12-23
  • 2021-10-23
  • 2021-08-11
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-07
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案