gpt4 book ai didi

c# - : "save picture as" in IE for an image retrieved from database via an image handler 问题

转载 作者:行者123 更新时间:2023-11-30 22:26:26 25 4
gpt4 key购买 nike

我有一个包含 aspImage 控件的网页当我通过图像处理程序从数据库中检索图像时,我可以在 aspImage 控件中显示该图像!但 。 . .问题是当我右键单击图像并单击“将图片另存为”时,我收到此错误消息:正在保存或检索的文件类型已被阻止!我想知道这是 IE 问题还是我的代码有问题?!有什么帮助吗?!

这是我的图像处理程序代码:

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

using System;
using System.Configuration;
using System.Data.SqlClient;
using System.Web;

public class Handler : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{

SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString);
public void ProcessRequest(HttpContext context)
{
string TableName=context.Session["TableToQuery"].ToString();
string ID=context.Session["ID"].ToString();

SqlCommand comm = new SqlCommand("SELECT * FROM "+ TableName +" WHERE ID=" + ID, conn);

conn.Open();
SqlDataReader dr = comm.ExecuteReader();
dr.Read();
context.Response.BinaryWrite((byte[])dr["Image"]);
conn.Close();
}

public bool IsReusable
{
get
{
return false;
}
}

}

这是我展示图像的数据列表:

<asp:DataList ID="DL" runat="server" Width="100%" Height="100%" RepeatColumns="1"
RepeatDirection="Vertical">
<ItemTemplate>
<table style="height: 100%; width: 100%">
<tr style="width: 100%; height: 350px">
<td valign="middle">
<asp:Image ID="IMage" runat="server" ImageUrl='<%# "Handler.ashx?ID=" + Eval("ID") %>' />
</td>
</tr>
<tr style="width: 100%; height: 250px">
<td valign="top">
<asp:TextBox ID="txtDecoded" runat="server" TextMode="MultiLine" Width="316px"
Height="200px" Text='<%#Eval("DecodedString")%>'></asp:TextBox>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>

最佳答案

您没有发送任何 header 来声明内容的 MIME 类型,只是一个 context.Response.BinaryWrite

至少你应该在 BinaryWrite 之前添加这样的东西

context.Response.ContentType = "image/jpeg";

关于c# - : "save picture as" in IE for an image retrieved from database via an image handler 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11807703/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com