我不明白为什么当我试图输出下面的图像和一行文本时,我只得到了图像,我做错了什么?
SqlConnection cn = new SqlConnection("CONNECTIONINFO HERE");
SqlCommand cmd = new SqlCommand("SELECT * FROM test WHERE id=" + Request.QueryString["id"], cn);
cn.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read()) //check to see if image was found
{
Response.ContentType = dr["fileType"].ToString();
Response.BinaryWrite((byte[])dr["imagesmall"]);
Response.Write("<br/>This is your image, if this is incorrect please contact the administrator.");
}
cn.Close();发布于 2011-03-15 02:13:37
为您的镜像创建一个HttpHandler,并从镜像标签调用它。
如下所示:
public class ImageHandler : IHttpHandler
{
public bool IsReusable
{
get { return true; }
}
public void ProcessRequest(HttpContext context)
{
HttpRequest request = context.Request;
HttpResponse response = context.Response;
SqlConnection cn = new SqlConnection("CONNECTIONINFO HERE");
SqlCommand cmd = new SqlCommand("SELECT * FROM test WHERE id=" + request.QueryString["id"], cn);
cn.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read()) //check to see if image was found
{
response.ContentType = dr["fileType"].ToString();
response.BinaryWrite((byte[])dr["imagesmall"]);
}
cn.Close();
}
}然后像这样使用它:
<img src="/ImageHandler.axd?id=1" />
<p>This is your image, if this is incorrect please contact the administrator.</p>您需要在web.config或IIS7中配置处理程序。
下面是如何实现的:http://mvolo.com/blogs/serverside/archive/2007/08/15/Developing-IIS7-web-server-features-with-the-.NET-framework.aspx
发布于 2011-03-15 02:12:51
好吧..。内容类型可能被设置为类似于jpg、gif或某种其他图像类型。实际上,我很惊讶浏览器显示的是图像,而不是错误。
关键是,你不能混合响应类型。
如果您必须显示文本,则需要发出一个常规的HTML文件,并在底部使用一个image标记显示您的图像和相关文本。
发布于 2011-03-15 02:12:03
您正在将内容类型设置为二进制图像类型。最有可能的是浏览器忽略了文本。如果您希望在响应中同时包含这两种类型,则可能必须使用text/html作为响应类型,并使用<img>标记内联图像数据。
有关内联图像数据的示例,请参阅here。
https://stackoverflow.com/questions/5302556
复制相似问题