VS2005中FileUpload怎样选中图片时,实现预览效果。FileUpload是服务器控件。高手,求救啊。。

解决方案 »

  1.   

    不行,能否贴出代码,label显示的还是选中的绝对路径
      

  2.   

     string type;
            if (ddlist.SelectedItem.Value == "")
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('你还没有选择专栏');window.location.href='User.aspx';", true);
               
                return;
            }
            else
            {
                type = ddlist.SelectedItem.Value;
                ViewState["type"] = type;
                ddlist.Enabled = false;
            }
            Boolean fileOK = true;
            if (fUfile.HasFile)
            {
                if (fileOK)
                {
                    try
                    {
                        string Date = System.DateTime.Now.ToString("yyMMddhhmmss");
                        string subFie = fUfile.FileName.Substring(fUfile.FileName.LastIndexOf("."), 4);
                        string sLocalPath = "../common/wwjeditor/UploadFiles/";
                        string folder = Server.MapPath(sLocalPath)  + type;                    if (!Directory.Exists(folder))
                            Directory.CreateDirectory(folder);
                       
                        string path = folder + "\\" + Date + subFie;                    string fileName = fUfile.FileName;
                        int size = fUfile.PostedFile.ContentLength;
                       // string userName = type;                    Multimedia mul = new Multimedia();
                        mul.Name = fileName;
                        mul.Size = size;
                        mul.Path = "common/wwjeditor/UploadFiles/" + type + "/" + Date + subFie;
                        mul.Series = 1;                    //上传到文件夹
                        sLocalPath = "../common/wwjeditor/UploadFiles/" + type;
                        path = Server.MapPath(sLocalPath) + "\\" + mul.Path.Substring(mul.Path.LastIndexOf("/") + 1);
                        if (!File.Exists(path))
                        {
                            fUfile.SaveAs(path);
                        }
                             if (ViewState["list"] != null)
                        {
                            List<Multimedia> list =(List<Multimedia>) ViewState["list"];
                            mul.Series = list.Count + 1;
                            list.Add(mul);
                            gvAccessory.DataSource = list;
                            gvAccessory.DataBind();
                        }
                        else
                        {
                            List<Multimedia> list = new List<Multimedia>();
                            list.Add(mul);
                            ViewState["list"] = list;
                            gvAccessory.DataSource = list;
                            gvAccessory.DataBind();
                        }                }
                    catch (System.Exception ex)
                    {
                        Response.Write("<script>alert('" + ex.Message + "');</script>");
                        return;
                    }
                }
            }
            else
            {
                Response.Write("<script>alert('上传的文件不符合要求!');</script>");
                return;
            }
      

  3.   

    二进制图片显示上传二进制
    if (FileUpLogo.HasFile)
    {
      //取得上传文件的大小
      int FileLen = FileUpLogo.PostedFile.ContentLength;
      Byte[] FileData = new Byte[FileLen];
      //创建访问客户端上传文件的对象
      HttpPostedFile hp = FileUpLogo.PostedFile;
      //创建数据流对象
      System.IO.Stream sr = hp.InputStream;
      //将图片数据放到FileData数组对象实例中,0代表数组指针的起始位置,FileLen代表指针的结束位置
      sr.Read(FileData, 0, FileLen);
      //将FileData 赋值给实体
      brandModel.fld_logo = FileData;
    }
    或者HttpPostedFile upFile = up_file.PostedFile;//HttpPostedFile对象,用来读取上传图片的属性
                fileLength = upFile.ContentLength;//记录文件的长度
       try
       {
        if(fileLength==0)//当文件长度为0的时候
        {
         txtMessage.Text = "请选择要上传的文件!";
        }
        else
        {
         byte[] fileByte = new byte[fileLength];//用图片的长度来初始化一个字节数组存储临时的图片文件
         Stream fileStream = upFile.InputStream;//建立文件流对象
         fileStream.Read(fileByte,0,fileLength);//读取图片数据到临时存储体fileByte,0为数据指针位置,fileLength为数据长度
         string connString = "Data Source=192.168.1.250;database=image;uid=pwqzc;pwd=cn0088";
         SqlConnection conn = new SqlConnection(connString);//初始化数据库连接
         string insertStr = "insert into image (image_data,image_content_type,image_description,image_size) values (@image_data,@image_content_type,@image_description,@image_size)";
         //插入数据库语句
         SqlCommand comm = new SqlCommand(insertStr,conn);
         comm.Parameters.Add(new SqlParameter("@image_data",SqlDbType.Image));//添加参数
         comm.Parameters["@image_data"].Value = fileByte;//给参数赋值
         comm.Parameters.Add(new SqlParameter("@image_content_type",SqlDbType.VarChar,50));
         comm.Parameters["@image_content_type"].Value = upFile.ContentType;//记录图片类型
         comm.Parameters.Add(new SqlParameter("@image_description",SqlDbType.VarChar,50));
         comm.Parameters["@image_description"].Value = txtDescription.Text;//把其他的表单数据上传
         comm.Parameters.Add(new SqlParameter("@image_size",SqlDbType.Int,4));
         comm.Parameters["@image_size"].Value = upFile.ContentLength;//记录图片长度,读取数据的时候使用
         conn.Open();//打开数据库连接
         comm.ExecuteNonQuery();//添加数据
         conn.Close();//关闭数据库
         txtMessage.Text = "你已经成功的上传了图片";
        }
       }
       catch(Exception ex)
       {
           txtMessage.Text = ex.Message.ToString();
       }
      }
     }读取
    using(SqlConnection conn=new SqlConnection())  
    {  
    conn.ConnectionString="";  
       
    string strSql="select * from Tb where Id='"+Id+"'";  
    SqlCommand cmd=new SqlCommand(strSql,conn) ;  
    conn.Open();  
    SqlDataReader reader=cmd.ExecuteReader();  
       
    if(reader.Read())  
    {  
    Response.ContentType = "application/octet-stream";  
    Response.BinaryWrite((Byte[])reader["Photo"]);  
    }  
    Response.End();  
    conn.Close();