I have a generic handler
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "image/jpeg,png,jpg,gif";
int newsId = int.Parse(context.Session["newsId"].ToString());
int FK_UnitId = int.Parse(context.Session["UserData"].ToString());
Managers.Photo p = new Managers.Photo();
string dirPath = ConfigurationManager.AppSettings.GetValues("ThePath").First() + "/" + "NewsImages" + "/" + "UnitNum" + FK_UnitId.ToString() + "_" + "NewsNum" + newsId.ToString() + "/";
string dirPathForTextFiles = ConfigurationManager.AppSettings.GetValues("ThePath").First() + "/" + "NewsTextFiles" + "/" + "UnitNum" + FK_UnitId.ToString() + "_" + "NewsNum" + newsId + "/";
DataTable dt = p.GetAllPhotos(newsId);
List<string> l = new List<string>(dt.Rows.Count);
byte[] b = null;
FileStream f;
try
{
for (int i = 0; i < dt.Rows.Count; i++)
{
l.Add(dirPath + dt.Rows[i]["photoName"].ToString());
f = new FileStream(l[i].ToString(), FileMode.Open, FileAccess.ReadWrite);
b = new byte[f.Length];
f.Read(b, 0, b.Length);
context.Response.OutputStream.Write(b, 0, b.Length);
context.ClearError();
f = null;
b = null;
}
}
catch (IOException e)
{
string message = e.Message;
}
}
and a list view on my page: the .cs file contains
Session.Add("newsId", newsId);
string dirPath =ConfigurationManager.AppSettings.GetValues("ThePath").First() + "/" + "NewsImages" + "/" + "UnitNum" + FK_UnitId.ToString() + "_" + "NewsNum" + newsId + "/";
string dirPathForTextFiles =ConfigurationManager.AppSettings.GetValues("ThePath").First() + "/" + "NewsTextFiles" + "/" + "UnitNum" + FK_UnitId.ToString() + "_" + "NewsNum" + newsId + "/";
DataTable dt = p.GetAllPhotos(int.Parse(newsId));
List<string> l = new List<string>(dt.Rows.Count);
for (int i = 0; i < dt.Rows.Count; i++)
{
l.Add(dirPath + dt.Rows[i]["photoName"].ToString());
}
lv_showImages.DataSource = l;
lv_showImages.DataBind();
my source
<asp:ListView ID="lv_showImages" GroupItemCount="4" runat="server">
<ItemTemplate>
<asp:Image ID="img_newsImage" Height="100px" Width="100px" runat="server ImageUrl ='<%# "RetreiveImage.ashx" %>' />
</ItemTemplate>
</asp:ListView>`
Now my problem:
When the list has more than one path to set of images, only the first image has appeared and repeated, although I debug my handler and I find that length of b the byte array differs in the loop and this means that it is supposed to write different images rather than repeat the first image by the number of images, I don’t know why the first image only appeared.
I don’t think you can return multiple images in a single request. I’m guessing that you are writing the contents of all of your images to the response but the browser only reads up to the end of the first image. You will have to change your handler to accept an image identifier parameter and return only that image.