将图像从Sql数据库获取到图像控件的问题 [英] Problems getting an image from Sql database into image control

查看:88
本文介绍了将图像从Sql数据库获取到图像控件的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好。想知道是否有人可以就我可能做错的事情提出任何想法。我有一个包含图像作为图像文件的sql数据库(image.jpg)。我还将这些相同的图像存储在我的应用程序中的Uploads文件夹中。我正在尝试从数据库中检索图像,具体取决于用户在上一页中选择的ID并将其显示在表格中的图像控件中,但我无法使其正常工作。表中的其他字段返回正常(txtName,txtTitle,txtCopy)。只是图像不起作用。我不确定的是,如果我需要后面的代码以及控件本身中的内容来提供图像。任何帮助都表示赞赏。



<表> 
< tr>< td class =AddArticleLabel>文章名称:< / td>
< td class =AddArticleTextbox>< asp:TextBox ID =txtNameclass =AddArticleTextboxrunat =server/>< / td>
< / tr>

< tr>< td class =AddArticleLabel>文章标题:< / td>
< td class =AddArticleTextbox>< asp:TextBox ID =txtTitleclass =AddArticleTextboxrunat =server/>< / td>
< / tr>

< tr>
< td>< asp:图片ID =Image1runat =server
ImageUrl ='<%#ResolveUrl(〜/ Uploads /+ Eval(Thumbnail)) %>'/>< / td>
< / tr>

< tr>< td class =AddArticleLabel>文章副本:< / td>
< td class =CopyTextbox>< asp:TextBox ID =txtCopyheight =100pxwidth =300pxrunat =server/>< / td>
< / tr>

< / table>





我的代码背后:

 protected void Page_Load(object sender,EventArgs e)
{
if(!Page.IsPostBack)
{
string ArticleID =;
if(Request.QueryString [ArticleID]!= null)
{
ArticleID = Request.QueryString [ArticleID];

if(Request.QueryString [delete] ==yes)
{
ArticleDataAccessLayer adal = new ArticleDataAccessLayer();
adal.DeleteArticle(ArticleID);
Response.Redirect(AllArticles.aspx);
}

文章a = new ArticleDataAccessLayer()。GetArticle(ArticleID);

txtName.Text = a.Name;
txtTitle.Text = a.Title;
txtCopy.Text = a.Copy;

//我需要这样的东西吗?我试过了,虽然它没有用。

Image1.ImageUrl = a.Thumbnail;

hfArticleID.Value = a.ArticleID.ToString();

}

}
}

解决方案

如果图像来自数据库,那么您需要一个HTTP处理程序来查找图像字节并返回它们以响应图像请求。如果您的文件系统上有图像,则需要将URL作为ImageUrl传递到Web服务器文件系统中的图像。两种方式存储都没有任何意义,我可以看到。将URL路径存储在DB中。


Hi guys. Wondering if anyone can offer any ideas on what I may be doing wrong. I have a sql database that contains images as image files (image.jpg). I also have these same images stored in my application in an Uploads folder. I'm trying to retrieve the image from the db depending on the id the user selects in the previous page and show it in the image control in a table, but I can't get it working. The other fields in the table come back fine (txtName, txtTitle, txtCopy). Just the image isn't working. What I'm not sure about is if I need something in the code behind as well as in the control itself to bring the image in. Any help is appreciated.

<table>
            <tr><td class="AddArticleLabel">Article name: </td>
                <td class="AddArticleTextbox"><asp:TextBox ID="txtName" class="AddArticleTextbox" runat="server" /></td>               
            </tr>

            <tr><td class="AddArticleLabel">Article title: </td>
                <td class="AddArticleTextbox"><asp:TextBox ID="txtTitle" class="AddArticleTextbox" runat="server" /></td>
            </tr>

            <tr>
                <td><asp:Image ID="Image1" runat="server"
                    ImageUrl='<%#ResolveUrl("~/Uploads/"+Eval("Thumbnail")) %>' /></td>
            </tr>

            <tr><td class="AddArticleLabel">Article copy: </td>
                <td class="CopyTextbox"><asp:TextBox ID="txtCopy" height="100px" width="300px" runat="server" /></td>
            </tr>           

        </table>



And my code behind:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        string ArticleID = "";
        if (Request.QueryString["ArticleID"] != null)
        {
            ArticleID = Request.QueryString["ArticleID"];

            if (Request.QueryString["delete"] == "yes")
            {
                ArticleDataAccessLayer adal = new ArticleDataAccessLayer();
                adal.DeleteArticle(ArticleID);
                Response.Redirect("AllArticles.aspx");
            }

            Articles a = new ArticleDataAccessLayer().GetArticle(ArticleID);

            txtName.Text = a.Name;
            txtTitle.Text = a.Title;
            txtCopy.Text = a.Copy;

            // Do I need something like this? I've tried and it didn't work though.

            Image1.ImageUrl = a.Thumbnail;

            hfArticleID.Value = a.ArticleID.ToString();

        }

    }
}

解决方案

If the image comes from the DB, then you need a HTTP handler to look up the image bytes and return them in response to the image request. If you have the image on your file system, then you need to pass a URL to the image in your web server's file system as the ImageUrl. Storing it both ways makes no sense that I can see. Store the URL path in the DB.


这篇关于将图像从Sql数据库获取到图像控件的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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