当前位置:编程学习 > C#/ASP.NET >>

大家帮忙已经一天了,.ashx返回图片就是不显示

这里是ashx
<%@ WebHandler Language="C#" Class="Handler" %>

using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Configuration;
public class Handler : IHttpHandler {
    
    public void ProcessRequest (HttpContext context)
    {
        context.Response.ContentType = "text/gif";
        string id = (string)context.Request["id"];
        my_db.my_data_type type = (my_db.my_data_type)Enum.Parse(typeof(my_db.my_data_type), ConfigurationManager.AppSettings["dbType"]);
        if(id!=null)
        {
            MemoryStream imgStream;
            try
            {
                conn.Open();
                IDbCommand cmd = my_db.db.CreateCommand("select photo from employees where employeeid='"+id+"'",type,conn);
                byte[] blob = (byte[])cmd.ExecuteScalar();
                imgStream = new MemoryStream(blob);
                Image img = Image.FromStream(imgStream);
                context.Response.Write(img);  
            }
            finally
            {
                imgStream.Close();
            }
        }
    }
    public bool IsReusable {
        get {
            return true;
        }
    }

}

这里是显示图片
<asp:Image ID="Image1" runat="server" Height="80px" ImageUrl='<%# "img.ashx?id="+Eval("id") %>'                                                           Width="80px" />

好心人帮帮忙吧,我的神经已经脆弱了 --------------------编程问答-------------------- ImageUrl=这里直接输出路径不就得了?
难道你数据库存的是二进制的图片?
如果是,用Response.OutputStream.Write方法来输出 --------------------编程问答-------------------- 是的我的文件就是二进制的
能写下比较全的方法吗? --------------------编程问答-------------------- context.Response.OutputStream.Write用这个输出还是 X --------------------编程问答-------------------- context.Response.ContentType = "image/jpg";
"image/jpg"后面的值改下应该可以 --------------------编程问答-------------------- 加在context.Response.Write前面 --------------------编程问答-------------------- HttpContext.Current.Response.ContentType = "image/jpeg";
img.Save(HttpContext.Current.Response.OutputStream, ImageFormat.Jpg);
--------------------编程问答-------------------- public void ProcessRequest (HttpContext context)
    {
        string id = (string)context.Request["id"];
        my_db.my_data_type type = (my_db.my_data_type)Enum.Parse(typeof(my_db.my_data_type), ConfigurationManager.AppSettings["dbType"]);
        if(id!=null)
        {
            MemoryStream imgStream;
            try
            {
                my_db.Data dd = new my_db.Data();
                byte[] blob = (byte[])dd.Execute_Scalar("select img from enterprise where id='" + id + "'");
                imgStream = new MemoryStream(blob);
                Image img = Image.FromStream(imgStream);
                HttpContext.Current.Response.ContentType = "image/jpeg";
                img.Save(HttpContext.Current.Response.OutputStream, ImageFormat.Jpg);                
            }           
            finally
            {
                imgStream.Close();
            }
        }
    }
    public bool IsReusable {
        get {
            return false;
        }
    }
请看电脑,还是显示不出来
补充:.NET技术 ,  ASP.NET
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,