本文介绍了当我通过使用Handler从数据库获取图像时,我有一个小问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




当我通过使用Handler从数据库获取图像时我有一个小问题



In本地代码执行成功但是当代码在hostgator服务器上传时,我得到无法加载给定的URL



ASPX代码

Hi
when i am get the image from database by using Handler here i have a small Problem

In Local this code is executed successfully but when that code is uploaded in hostgator server I am getting The "Failed to load the Given Url"

The ASPX Code

<asp:DataList ID="DataList1" runat="server"

            RepeatColumns="3" RepeatDirection="Horizontal">
            <ItemTemplate>
                <table>
                    <tr>
                        <td valign="middle" align="center" style="background-color:#cccccc;border:1px solid gray;width:150px;height:150px;"><%#DataBinder.Eval(Container.DataItem, "images") %></td>
                    </tr>
                </table>

            </ItemTemplate>
        </asp:DataList>



ASPX.CS代码


The ASPX.CS Code

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
        BindDataList();
}
private void BindDataList()
{
    string sqlCmd = "SELECT carprofile FROM ca";
    DataBaseHelper DBHelper = new DataBaseHelper();
    DataTable dt = DBHelper.GetTable(sqlCmd);
    //adding new column to disply image
    DataColumn imageCol = new DataColumn("images", typeof(string));
    dt.Columns.Add(imageCol);        
    if (dt.Rows.Count > 0)
    {
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            dt.Rows[i][imageCol] = string.Format("<img src='Handler.ashx?id={0}' alt='' style='width:100px' />", dt.Rows[i][0].ToString());
        }
    }
    DataList1.DataSource = dt;
    DataList1.DataBind();
}



处理程序代码


Handler code

<%@ WebHandler Language="C#" Class="Handler" %>




using System;
using System.Web;
using System.Collections;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
public class Handler : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        HttpRequest request = context.Request;
        if (!string.IsNullOrEmpty(request.QueryString["id"]))
        {
            //this hash table contain the SP parameter
            Hashtable hash = new Hashtable();
            hash.Add("@carprofile", request.QueryString["id"]);
            DataBaseHelper DBHelper = new DataBaseHelper();

            //DBHelper.SQLExecuteNonQuery(procedure_name,command_parameters) return the object data.
            // casting return value to byte[]
            byte[] imageByte = (byte[])DBHelper.SQLExecuteNonQuery("Get_images", hash);
            //checking byte[] 
            if (imageByte != null && imageByte.Length > 0)
            {
                context.Response.ContentType = "image/jpeg";
                context.Response.BinaryWrite(imageByte);
            }
        }
    }
    public bool IsReusable {
        get {
            return false;
        }
    }

}



Plzzz给我这个解决方案





谢谢


Plzzz Give me the Solution for this


Thank you

推荐答案


这篇关于当我通过使用Handler从数据库获取图像时,我有一个小问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 18:11