注册 登录
编程论坛 ASP.NET技术论坛

有谁会FileUpload上传图片到imge里面的吗?

ruankai1036 发布于 2011-11-08 22:33, 1145 次点击
有谁会FileUpload上传图片到imge里面的吗?教教我啊!!!
7 回复
#2
winners2011-11-09 16:30
数据库中的image中吗?先将图片转成二进值,然后再插入
#3
ruankai10362011-11-10 12:22
回复 2楼 winners
怎么转换呀!!
#4
xun8142011-11-14 17:37
这个较为简单,多看看书,入门教程里面应该讲得很清楚
#5
qq9780203292011-11-17 02:17
转成二进制比较C#提供的方法很多,但插到数据库二进制内容变了,涉及到编码问题;你要读出来还得解决编码问题,再从二进制转成图片,超麻烦。
建议保存图片路径,而却用数据库来保存图片是比较浪费的。
#6
winners2011-11-17 14:09
以下是引用ruankai1036在2011-11-10 12:22:31的发言:

怎么转换呀!!
HttpPostedFile photo = Upphoto.PostedFile;
        int imglength = photo.ContentLength;
        Byte[] photoarray = new Byte[imglength];
        Stream stream = photo.InputStream;
        stream.Read(photoarray, 0, imglength);
        //Response.BinaryWrite(photoarray);
#7
白色艾艾2011-11-28 13:08
  protected void Button1_Click(object sender, EventArgs e)
    {
        Boolean fileOK = false;
        string path = Server.MapPath("~/UpLoadFile控件使用/UploadedImages/");
        if (FileUpload1.HasFile)
        {
            string fileExtension = (FileUpload1.FileName);
            string[] allowedExtensions = {".jpg",".jpeg",".txt",".png",".doc",".ppt",".gif"};
            for (int i = 0; i < allowedExtensions.Length; i++)
            {
                if (fileExtension == allowedExtensions[i])
                {
                    fileOK = true;
                }
            }
        }
        if (fileOK)
        {
            try
            {
                FileUpload1.PostedFile.SaveAs(path + FileUpload1.FileName);
                string picpath = "UpLoadFile控件使用/UploadedImages/" + FileUpload1.FileName;
                string proname = TextBox1.Text;
                string prodec = TextBox2.Text;
                DateTime date = DateTime.Now;
                string settings = Convert.ToString(ConfigurationManager.ConnectionStrings["sqlserver"]);
                SqlConnection con = new SqlConnection(settings);
                con.Open();
                string sql = "insert into  Products(ProName,Adddate,Description,imgurl) values('" + proname + "','" + date + "','" + prodec + "','" + path + "')";
                SqlCommand cmd = new SqlCommand(sql, con);
                cmd.ExecuteNonQuery();
                con.Close();
                Response.Write("<script>alert('商品添加成功!')</script>");
            }
            catch (Exception ex)
            {
                Response.Write("<script>alert('文件上传出现问题!')</script>");
            }
        }
        else
        {
            Response.Write("<script>alert('上传文件类型错误!')</script>");
        }
        TextBox1.Text = "";
        TextBox2.Text = "";
    }

然后你在建个查看图片的页面
    private void aa()
    {
        string settings = Convert.ToString(ConfigurationManager.ConnectionStrings["sqlserver"]);
        SqlConnection con = new SqlConnection(settings);
        con.Open();
        string sql = "select * from Products";
        SqlCommand cmd = new SqlCommand(sql,con);
        SqlDataReader da = cmd.ExecuteReader();
        FormView1.DataSource = da;
        FormView1.DataBind();
        con.Close();
    }
把图片绑定的formview控件中就行了
#8
牛腩2011-11-28 16:20
imge是文件夹吗?直接saveas 到那个文件夹路径就成了。
1