在ImageHandler.ashx.cs中动态处理来自DB的图像

时间:2014-08-06 13:19:40

标签: c# asp.net image imagehandler

我正在博客网站上工作。用户可以浏览'通过FileUpload控件并选择要与其帖子一起上传的多个图像。每个帖子在数据库中都有唯一的消息ID。我已经想出如何在一个消息ID下将所有上传的文件插入到数据库中,如下所示:(我将它们作为数据类型插入'图像')

 try
            {
                conn.Open();
                SqlCommand cmdInsert = new SqlCommand("INSERT INTO BlogMessages (Title, Message, Date, Author, IPAddress) VALUES (@Title, @Message, @Date, @Author, @IPAddress)", conn);
                cmdInsert.Parameters.AddWithValue("@Title", tbTitle.Text.Trim());
                cmdInsert.Parameters.AddWithValue("@Message", tbMessage.Text.Trim());
                cmdInsert.Parameters.AddWithValue("@Date", DateTime.Now);
                cmdInsert.Parameters.AddWithValue("@Author", tbAuthor.Text.Trim());
                cmdInsert.Parameters.AddWithValue("@IPAddress", ip);
                cmdInsert.ExecuteNonQuery();

                if (imageUpload.HasFiles)
                {
                    System.Text.StringBuilder sb = new System.Text.StringBuilder();
                    HttpPostedFile file;

                    SqlCommand maxMessId = new SqlCommand("SELECT Max(MessageID) FROM BlogMessages", conn);
                    lastMessageID = Convert.ToInt32(maxMessId.ExecuteScalar());

                    foreach (var uploadedFile in imageUpload.PostedFiles)
                    {
                        file = uploadedFile; 
                        MemoryStream ms = new MemoryStream();
                        file.InputStream.CopyTo(ms);
                        byte[] byts = ms.ToArray();
                        ms.Dispose();

                        SqlCommand cmdInsertImage = new SqlCommand("INSERT INTO BlogImages(Image, MessageID) VALUES (@Image, @MessageID)", conn);
                        cmdInsertImage.Parameters.AddWithValue("@Image", SqlDbType.Image).Value = byts;
                        cmdInsertImage.Parameters.AddWithValue("@MessageID", lastMessageID);
                        cmdInsertImage.ExecuteNonQuery();
                        byts = null;
                        cmdInsertImage = null; 


                    }

                }

但是,我无法动态检索图像以便在我的ImageHandler中显示。似乎我能够将所有图像都放在特定的MessageID下并将它们插入到数据集中,但之后我正在寻找指导,而我无法通过研究找到一个可行的解决方案迄今。以下是ImageHandler.ashx.cs的public void Process Request中的相关代码:

  try
            {
                int messageid = Convert.ToInt32(context.Request.QueryString["mid"]);

                SqlDataAdapter sda = new SqlDataAdapter("SELECT Image from BlogImages WHERE Image IS NOT NULL AND MessageID=" + messageid, conn);
                System.Data.DataSet data = new DataSet();
                sda.Fill(data);


                if (data !=null)
                {

                    byte[] result = (byte[])data.Tables[0].Rows[0]["ImageContent"];
                    result.Initialize();
                    context.Response.ContentType = data.Tables[0].Rows[0]["ImageExt"].ToString();
                    context.Response.BinaryWrite(result);

                }
                else
                {
                    context.Response.WriteFile("No Image");
                }
            }

谢谢!

0 个答案:

没有答案