Domanda

I have a Profile aspx file and there is fileupload button to upload an profile photo. When you click the file upload button, it opens the file selector window. when the user select the file and close that file selector window, the main page gets submitted. The photo get uploaded succesfully. But when the page gets loaded again, I see that the photo in masterpage doesnt change until another refreshing.

here is the code that load the photo to the masterpage from child,

        dbCommand = db.GetStoredProcCommand(Select_Users_Photo");
        db.AddInParameter(dbCommand, "user_id", DbType.Guid, new Guid(Session["SessionUserId"].ToString().Trim()));
        IDataReader dr = db.ExecuteReader(dbCommand);
        if (dr.Read())
        {
            image_user.ImageUrl = dr["PhotoPath"].ToString().Trim();
        }
        else
            image_user.ImageUrl = "images/man.jpg";
        dr.Close();
        dr.Dispose();
È stato utile?

Soluzione

If the caching this isn't the problem, it must be with your page lifecycle. It's impossible to tell based on what you have so far, but the only thing I can think of is that your code in the question (which sets the image URL) is wrapped in a if (!IsPostback), so it would not be updated upon upload, but would be updated upon a full GET load of the page.

Throw a breakpoint on that line and see when it actually gets hit.

Altri suggerimenti

Append the ?mtime=1257316941 at the end of the image url. If image exist in at the specified location. It will definitely show the updated image.

 if (dr.Read())
    {
        image_user.ImageUrl = dr["PhotoPath"].ToString().Trim()+ "?mtime=1257316941";
    }
    else
        image_user.ImageUrl = "images/man.jpg"+ "?mtime=1257316941";

Your upload process and retrieval process are probably just fine. The issue is going to be with the caching of the image. There are a few ways of dealing with this.

The easiest would be to add a random querystring parameter on the image tag - this way the browser would think that it's a different image every time, and would always request the new one from the server.

<img src="/path-to-image/img.png?randomval=<%= Guid.NewGuid() %>">

So from your code, it would be:

image_user.ImageUrl = dr["PhotoPath"].ToString().Trim() + "?randomval=" + Guid.NewGuid();

There are other ways of preventing caching, but this is probably the fastest and easiest, without disrupting the rest of your page.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top