我有一个问题,我需要你的帮助:
我正在制作一个 Web 应用程序来访问我需要在类似( ListView 或 DataList )这样的工具中显示他们的员工列表,以直接绑定(bind)数据库中的数据,并且数据库中有一个字段保存为图像我需要用我在这里提到的细节来展示这张图片
然后我创建一个 ASP 处理程序来流式传输图像,它采用查询字符串(“代码”),它的返回字节流包括“data:image/png;base64,image”
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TimeSheet.DstTableAdapters;
namespace TimeSheet
{
/// <summary>
/// Summary description for emp_img
/// </summary>
public class emp_img : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
string emp_code = context.Request.QueryString["code"];
string output_img = "";
string no_image = "i remove the image code from here ";
string male = "i remove the image code from here ";
if (emp_code == null || emp_code == "")
{
output_img = no_image;
}
else
{
try
{
EmployeeTableAdapter adp = new EmployeeTableAdapter();
Dst.EmployeeDataTable all_emps = adp.get_by_code(emp_code);
foreach (Dst.EmployeeRow emp in all_emps)
{
if (emp.Data == null)
{
//here to add the users without image
output_img = male;
}
else
{
byte[] bytes = emp.Data;
string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
output_img = "data:image/png;base64," + base64String;
}
}
}
catch (Exception)
{
output_img = no_image;
}
context.Response.BinaryWrite(GetBytes(output_img));
context.Response.End();
}
}
然而,当我尝试在我的代码中以这种方式使用它时......
<asp:ListView ID="ListView1" runat="server" DataSourceID="myteamdata" ItemPlaceholderID="placeholder" OnItemInserting="ListView1_ItemInserting" >
<LayoutTemplate>
<div class="row">
<div class="col-lg-12" id="placeholder" runat="server">
</div>
</div>
<asp:DataPager ID="DataPager1" runat="server" PageSize="12"></asp:DataPager>
</LayoutTemplate>
<ItemTemplate>
<div class="col-lg-3">
<asp:Image ID="Image1" runat="server" ImageUrl='<%# "emp_img.ashx?code="+ Eval("Code") %>'/>
</div>
</ItemTemplate>
</asp:ListView>
<asp:ObjectDataSource ID="myteamdata" runat="server" OldValuesParameterFormatString="original_{0}" SelectMethod="get_all_data" TypeName="TimeSheet.DstTableAdapters.EmployeeTableAdapter"></asp:ObjectDataSource>
那么解决方案是什么来处理来自数据绑定(bind)的输出,例如 c# 代码中的(bind 或 Eval),以及我如何调用方法在某些类中进行一些计算到使用该数据绑定(bind)的 ASP 页面中结果作为该方法的输入
非常感谢您的提前帮助
最佳答案
你的图像处理器返回的是一个字符串,
byte[] bytes = emp.Data;
string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
output_img = "data:image/png;base64," + base64String;
但是img
可以显示的是一个真正的二进制文件/数据
将您的内容类型更改为
context.Response.ContentType = "image/png";
context.Response.BinaryWrite(emp.Data);
并按原样发送您的数据。
你有更多的错误,你不存在的图像必须是磁盘上的图像文件......我不知道还有什么......返回图像的 foreach
也不好如果你找到了不止一张图片...
关于c# - 数据绑定(bind)误解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37641714/