Dynamic image generation gives red x's after ~50 images
I have a project that needs to generate a random collection of images on an html page. Everything works fine until around the 50th image is generated and then I just get a red X.
Am I hitting some connection limit or server resource limit?
The code below exhibits the behavior on several different machines. I am using Visual Studio 2010 and Internet Explorer.
Thanks!
HTML Page:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
<div id="images">
</div>
</body>
<script>
var count = 0;
function AppendImage() {
var img = document.createElement("img");
img.src = "DynamicImage.aspx?id=" +开发者_JS百科 count++;
document.getElementById("images").appendChild(img);
img.onreadystatechange = function() {
if (this.readyState == "complete") {
AppendImage()
}
}
}
AppendImage();
</script>
</html>
DynamicImage.aspx
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
System.IO.FileStream fs = new System.IO.FileStream("d:\\png.png", System.IO.FileMode.Open, System.IO.FileAccess.Read);
IntPtr handle = fs.Handle;
long size = fs.Length;
Response.ContentType = "image/png";
Response.WriteFile(handle, 0, size);
fs.Close();
}
</script>
so from what i can tell this is a recursive function call that never ends. is that wise?
have you tried making a non-recursive function and see if you have better luck?
Try using Response.Clear() first, and Response.End()
protected void Page_Load(object sender, EventArgs e)
{
Response.Clear();
System.IO.FileStream fs = new System.IO.FileStream("d:\\png.png", System.IO.FileMode.Open, System.IO.FileAccess.Read);
IntPtr handle = fs.Handle;
long size = fs.Length;
Response.ContentType = "image/png";
Response.WriteFile(handle, 0, size);
fs.Close();
Response.End();
}
The issue here turned out to be a memory problem on the client machine. The image was of a size that when loaded ~50 times on a page caused Ineternet Explorer's memory to reach just shy of 2GB.
I verified this to be true by changing the test image to smaller and larger files and the same behavior appeared when the memory approached the 2GB mark.
精彩评论