如何从数据库中检索插入的上传图像并立即在页面中显示图像? [英] how can i retrive the inserted uploaded image from database and show the image instantly in the page?

查看:67
本文介绍了如何从数据库中检索插入的上传图像并立即在页面中显示图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨。



我已编写以下代码将图像插入数据库,然后将其重新显示并立即显示在页面中。



用于上传和插入步骤,它工作正常,但我无法回复&显示:

Hi.

I have written the following code to insert the image into the database and then retrive it and show it instantly in the page.

for uploading and inserting step , it works fine , but i can not retrive & show it:

protected void SendImageButton_Click(object sender, EventArgs e)
        {
            if (FileUpload.PostedFile != null && FileUpload.PostedFile.FileName != null)//Specify whether user has posted the file or not.
            {
                HttpPostedFile Picture = FileUpload.PostedFile;
                if (Picture.ContentLength==0) //Check the content of the file
                {
                    Response.Write("فایل شما هیچ محتوایی ندارد!");
                    return;
                }
                //check the path of the file that has to be valid form.
                //if (Path.GetExtension(Picture.FileName).ToLower()!=".jpg" || Path.GetExtension(Picture.FileName).ToLower()!=".jpeg" || Path.GetExtension(Picture.FileName).ToLower()!=".jpe" ||Path.GetExtension(Picture.FileName).ToLower()!=".jfif")
                //{
                //    Response.Write("پسوند این فایل نامعتبر است!");
                //    return;
                //}
                //convert the picture into an array form to enable it for inserting into the database, and then read the file.
                byte[] MyImage = new Byte[Picture.ContentLength];
                Picture.InputStream.Read(MyImage, 0, Picture.ContentLength);

                //Insert into the UplaodPicture table by using StoredProcedure.
                SqlCommand Command = DataProvider.GenerateCommand("[dbo].[Insert_UploadPicture_SP]", CommandType.StoredProcedure);
                Command.Parameters.AddWithValue("@IdCode", GlobalVariables.IdCode);
                Command.Parameters.AddWithValue("@ImageContent", MyImage);
                Command.Parameters.AddWithValue("@ImageSize", Picture.ContentLength);
                Command.Parameters.AddWithValue("@ImageType", Picture.ContentType);

                try
                {
                    Command.Connection.Open();
                    Command.ExecuteNonQuery();
                    Command.Connection.Close();
                }
                catch (Exception ex)
                {
                    EventLogger.Log(ex.Message, LogType.Error);
                }
                finally
                {
                    DataProvider.DisposeCommand(Command);
                }

            }
            //Read the inserted Image Array from database and show the Image inside the box.
            SqlCommand ShowCommand = DataProvider.GenerateCommand("[dbo].[Select_UploadPicture_SP]", CommandType.StoredProcedure);
            
            try
            {
                ShowCommand.Connection.Open();
                SqlDataReader DataReader = DataProvider.ExecuteDataReader("[dbo].[Select_UploadPicture_SP]", CommandType.StoredProcedure);
                DataReader.Read();
                PersonelImage.ImageUrl = DataReader["ImageContent"].ToString();
                DataReader.Close();
                ShowCommand.Connection.Close();
            }
            catch (Exception ex)
            {
                EventLogger.Log(ex.Message, LogType.Error);
            }
            finally
            {
                DataProvider.DisposeCommand(ShowCommand);
            }
        }



所以我的代码有什么问题?



i非常感谢你的帮助。


so what is the problem of my code?

i will be thankfull for your help.

推荐答案





你可以使用asp.net图像控制





you can use asp.net image control

PersonelImage.Src = "Your Image Path"; 





如果要显示要插入数据库的照片,请使用异步方法显示图像,使用ajax工具和更新面板。





If you want to show the photo immedaite to insert into database , then use asynchronous method to show the image , using the ajax toolscript and update panel .

<html>
<head runat="server">
    <script language="javascript">

        function Change(obj) {
            __doPostBack("<%= btnUpload.ClientID %>", "");
        }

    </script>
</head>
<body>
    <form id="form1" runat="server">
    Upload Image:
        <asp:FileUpload ID="imgFileUploader" runat="server"   />
            <asp:Button ID="btnUpload" runat="server"

        Text="Upload" onclick="btnUpload_Click" onchange="Change(this);" />
    <br />
       <br />
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
&nbsp;<asp:UpdatePanel ID="UpdatePanel1" runat="server">
           <ContentTemplate>
              <img id="imgOrginal" runat="server" style="height: 200px; width: 200px;" />
              </ContentTemplate>
    </asp:UpdatePanel>
    </form>
</body>
</html>







protected void btnUpload_Click(object sender, EventArgs e)
{
      imgFileUploader.PostedFile.SaveAs(Server.MapPath("~/image/" + imgFileUploader.FileName));
          imgOrginal.Src = "~/image/" + imgFileUploader.FileName;
}


你不能这样做。

You can''t do like this.
PersonelImage.ImageUrl = DataReader["ImageContent"].ToString();





您可以为此任务使用一个处理程序并将其分配给 ImageURL



You can use one handler for this task and assign that to the ImageURL.

// Display the image from the database
Image1.ImageUrl = "~/ShowImage.ashx?id=" + id;



关注使用ASP.NET 2.0和ASP.NET 3.5从数据库保存和检索图像 [ ^ ],这将指导你一步一步如何完成这项任务。



谢谢......


Follow Save and Retrieve Images from the Database using ASP.NET 2.0 and ASP.NET 3.5[^], which will guide you step by step how to do this task.

Thanks...


这篇关于如何从数据库中检索插入的上传图像并立即在页面中显示图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆