标题:有谁会FileUpload上传图片到imge里面的吗?
只看楼主
ruankai1036
Rank: 2
等 级:论坛游民
帖 子:26
专家分:40
注 册:2011-10-31
结帖率:66.67%
已结贴  问题点数:20 回复次数:7 
有谁会FileUpload上传图片到imge里面的吗?
有谁会FileUpload上传图片到imge里面的吗?教教我啊!!!
搜索更多相关主题的帖子: 图片 
2011-11-08 22:33
winners
Rank: 6Rank: 6
来 自:济南
等 级:侠之大者
威 望:1
帖 子:105
专家分:416
注 册:2009-3-20
得分:20 
数据库中的image中吗?先将图片转成二进值,然后再插入
2011-11-09 16:30
ruankai1036
Rank: 2
等 级:论坛游民
帖 子:26
专家分:40
注 册:2011-10-31
得分:0 
回复 2楼 winners
怎么转换呀!!
2011-11-10 12:22
xun814
Rank: 2
等 级:论坛游民
帖 子:23
专家分:83
注 册:2011-11-14
得分:0 
这个较为简单,多看看书,入门教程里面应该讲得很清楚
2011-11-14 17:37
qq978020329
Rank: 2
等 级:论坛游民
帖 子:3
专家分:10
注 册:2011-6-20
得分:0 
转成二进制比较C#提供的方法很多,但插到数据库二进制内容变了,涉及到编码问题;你要读出来还得解决编码问题,再从二进制转成图片,超麻烦。
建议保存图片路径,而却用数据库来保存图片是比较浪费的。
2011-11-17 02:17
winners
Rank: 6Rank: 6
来 自:济南
等 级:侠之大者
威 望:1
帖 子:105
专家分:416
注 册:2009-3-20
得分:0 
以下是引用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);
2011-11-17 14:09
白色艾艾
Rank: 2
等 级:论坛游民
帖 子:11
专家分:14
注 册:2011-11-28
得分:0 
  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控件中就行了
2011-11-28 13:08
牛腩
Rank: 3Rank: 3
来 自:南宁
等 级:论坛游侠
威 望:1
帖 子:59
专家分:161
注 册:2007-6-3
得分:0 
imge是文件夹吗?直接saveas 到那个文件夹路径就成了。
2011-11-28 16:20



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-354657-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.319481 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved