I have a web application into which I upload an image, the image is subsequently saved in the server temp folder and displayed through the web manipulator on the aspx page.
code for aspx:
<img src="PreviewImageQualityHandler.ashx" alt="Picture not loaded" runat="server" id="imagePreview" />
Code to upload the image and add a unique identifier to the session:
protected void uploadButton_Click(object sender, EventArgs e)
{
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>");
}
}
Webmaster 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 handler only starts when the image is first loaded. If I try to upload a new image, it never works.
If I delete everything that needs to be done with Session from the web handler, it starts with every postback, as it should be.
If someone can shed light on my problem, I will be extremely grateful!
Hi
Francis