Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 531257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:16:56+00:00 2026-05-13T09:16:56+00:00

I have a web application that I upload an image to, the image is

  • 0

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)
{
    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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T09:16:57+00:00Added an answer on May 13, 2026 at 9:16 am

    OK, a rewrite of my answer (see history if you haven’t seen earlier answers). A few notes on your code:

    1. Don’t use Response.Write. You cannot determine when it will write and often you won’t see it in the browser. Use an asp:Literal control or an asp:Label instead. Related with current issue because: if you don’t see it in the browser, perhaps you think nothing happens.

    2. You don’t safe-guard the opening / closing of the file stream. Add a using-block around it so that the stream is properly closed and disposed off. The way you have it currently, the file will remain open as long as the thread is alive and that’s pretty long.

      using(Bitmap bmp = (Bitmap)System.Drawing.Image.FromFile(sessID))
      {
           //.. your code...
           using(MemoryStream.... etc)
           {
               ....
           }
      }
      

      The problem with not-closing: you will not be able to write a new file to the same location next time, you may not be able to open it for reading and you’ll quite quickly run out of available file handlers when your code goes live.

    3. Not sure what you expect from the session. But it is probably better to use some ID with the picture, store that in the database with the current user info, and add that as a request querystring to the image URL. That way, you can’t get into trouble with session issues:

      <img src="myimage.ashx?id=1234" />
      

      Either, 1234 refers to something on disk, or it refers to something in the database, which in turn refers to something on disk (much safer).

    4. If you are unsure whether the browser resends the request, use FireBug with FireFox, which gives you full details of the HTTP requests and responses. If you use the same name, the browser will (unless you’re in debug mode) not send a new request each time.

    5. You’re using a catch-all exception handler. Write something there, or place a breakpoint at that position. A catch-all exception handler is very dangerous: many exceptions will never be seen. Quite possibly, your current code throws an exception (see point 2 above) and that’s it. Remove the exception handler and the exception be caught by your Visual Studio debugger so you know what’s up.

    I know, quite some issues altogether. Go through them one by one and see if it helps. At the very least, your code will become more stable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 254k
  • Answers 254k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I mostly solved the redrawing problem using DoubleBuffered=True, so probably… May 13, 2026 at 10:10 am
  • Editorial Team
    Editorial Team added an answer No idea if this will work, but have you tried… May 13, 2026 at 10:10 am
  • Editorial Team
    Editorial Team added an answer The webapplication urls are bound to the Zones used by… May 13, 2026 at 10:10 am

Related Questions

I have a scenario where users of my ASP.NET web application submit testimonials consisting
I have developed an image uploading application that uses Flash to load an image,
I am working on a web application that will deal with many image uploads.
I have built a small web application in PHP where users must first log
I'm looking to implement a simple web-based application. The main reason I want to

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.