I have a web application that I upload an image to, the image is subsequently saved to the server's temp folder and displayed via a webhandler on the aspx page.
the code for the aspx:
<img src="PreviewImageQualityHandler.ashx" alt="Picture not loaded" runat="server" id="imagePreview" />
The code for uploading the picture and adding a unique id to the Session:
protected void uploadButton_Click(object sender, EventArgs e)
{
try
{
if (FileUploadControl.FileName.EndsWith(".jpg") || FileUploadControl.FileName.EndsWith(".jpeg"))
{
string tempFileName = Path.GetTempFileName();
FileUploadControl.SaveAs(tempFileName);
Session["tempName"] = tempFileName;
Response.Write(Session["tempName"]);
fileName = FileUploadControl.FileName;
}
else
{
Response.Write("<script>alert('Please select a .jpg/.jpeg image to upload')</script>");
}
}
}
The webhandler code:
public class PreviewImageQualityHandler : IHttpHandler, IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
try
{
if (context.Session.Count > 0)
{
string sessID = context.Session["tempName"].ToString();
Bitmap bmp = (Bitmap)System.Drawing.Image.FromFile(sessID);
context.Response.ContentType = "image/jpg";
MemoryStream ms = new MemoryStream();
bmp.Save(ms, ImageFormat.Bmp);
byte[] b = ms.ToArray();
context.Response.OutputStream.Write(b, 0, b.Length);
}
}
catch(Exception ex)
{
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
My problem is that the webhandler only fires on the first image-upload. If I try and upload a new image, the webhandler never fires.
If I remove everything to do with Session from the webhandler it fires on every single postback, like it should.
If anybody is able to shed some light on my problem, I will be extremely grateful!
Regards
Francis