使用response.write命令定位图像和图片 [英] positioning image and picture with response.write command

查看:86
本文介绍了使用response.write命令定位图像和图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难使用命令"response.write"将数据和图像放置在apx页上.当我要单独显示图像时,将显示它,但是当我同时显示数据和图像时,则只显示图像,但是我想在两个单独的行中显示图像和数据.这是我的代码:

I am in difficulties to position my data and image on a aspx page with the command "response.write". When I want to show image individually it is shown but when I want to both Data and Image, then only Image is shown, but I wanna show both Image and Data in two separate rows. Here is my Code:

protected void Page_Load(object sender, EventArgs e)
    {
        string productID = Request.QueryString["id"].ToString();
        string photoSize = Request.QueryString["Size"].ToString();
        System.Data.SqlClient.SqlDataReader imageReader;
        string dbConn = ConfigurationManager.ConnectionStrings["CCOConnectionString"].ConnectionString;
        System.Data.SqlClient.SqlConnection sqlConn = new System.Data.SqlClient.SqlConnection(dbConn);
        sqlConn.Open();
        System.Data.SqlClient.SqlCommand imageCommand = new System.Data.SqlClient.SqlCommand(
            "SELECT Picture, Message FROM Image "
                + "WHERE id=" + productID, sqlConn);
        imageReader = imageCommand.ExecuteReader(CommandBehavior.CloseConnection);
        imageReader.Read();
        
//For show Image: 
      if (photoSize == "ThumbNail")
        {
            byte[] imageBytes = (byte[])imageReader["Picture"];
            //Response.ContentType = "image/GIF";
            Response.ContentType = "image/JPEG";
            Response.BinaryWrite(imageBytes);
        }
        else if (photoSize == "Large")
        {
            byte[] imageBytes = (byte[])imageReader["Picture"];
            Response.ContentType = "image/JPEG";
            Response.BinaryWrite(imageBytes);
        }
        Response.Write("<br>");
        Response.Write("<br>");
        Response.Write("<br>");
        Response.Write("<br>");



//For Show Data: 
        Response.Write(imageReader["Message"]);
            imageCommand.Cancel();
            imageReader.Close();
            sqlConn.Close();
        }


<pre></pre>

请帮助我,我将感谢任何人
问候
Shah Noman


<pre></pre>

Plz help me, I will grateful to anyone
Regards
Shah Noman

推荐答案

好的一点,是图像大小始终被设置为ThumbNail还是Large,如果这样看来您浪费了代码行,并且可能,使自己感到困惑..

如果图像大小被建议为ThumbNail或Large,则始终将图像数据写入响应流.但是,您也始终将响应类型设置为image/JPEG,这意味着一旦写入了图像数据,就不能在响应中放入其他数据.这使得您尝试执行的操作变得不可能,因为您尝试在此之后写入后续文本数据(我假设是文本,但如果没有,则没有任何区别).一种简单的解决方案是有两页,一个响应图片,另一个响应图片的消息.大概您已经设置了AJAX或类似的请求图像,因此您也可以将其设置成请求消息数据.虽然,如果您只是简单地使用静态页面作为响应,而随后该页面随后由<img>标记请求图像,那么为什么不将文本放在页面的开头呢?到目前为止,这是最简单的解决方案.

通常,您不能在一个请求中使用两种不同的内容类型(MIME类型)进行响应.您不能发送图像,然后发送文本,也不能发送文本,然后发送文件,也不能发送任何其他组合,只能发送一种类型的请求.

希望这会有所帮助,

埃德:)
Okay one quick point, are the image sizes always garunteed to be ThumbNail or Large, if so you appear to have wasted lines of code and also, possibly, caused yourself confusion..

If the image sizes are garunteed to be ThumbNail or Large then you always write the image data to the response stream. However, you also always set the response type to image/JPEG which means that once you''ve written the image data you can put no other data in the response. This makes what your trying to do impossible as you are trying to write subsequent text data after (I assume text but it makes no difference if it isn''t). The simple solution is to have two pages, one that responds with the image and another with the picture''s Message. Presumably you''ve already got AJAX or similar set up to request the image so you could also set it up to request the Message data. Although, if you are simply responding with a static page initially where the image is subsequently requested by an <img> tag then why not put the text in the page to start with? That is by far the easiest soulution.

As a general point, you cannot respond with two different Content-types (Mime-types) in one request. You cannot send an image then text nor text then a file nor any other combination, only one type for one request.

Hope this helps,

Ed :)


这篇关于使用response.write命令定位图像和图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆