Caching Images after replace the images

 

Hi  

I  programatically upload an image . I can see that image in the asset pane at the CMS admin .But when i browse the page , it always showing the previous image after replace the image

 my code is

if (userProfileImage != null)
{
 newImage  =   (ImageFile)contentRepository.Get<ImageFile>(userProfileImage.ContentLink).CreateWritableClone();;

}

else{

newImage = contentRepository.GetDefault<ImageFile>(folder.ContentLink);
newImage.Name = userIfo.FirstName;

}

var byteArrayData = imageService.ToByteArray(image); ;

var blob = blobFactory.CreateBlob(newImage.BinaryDataContainer, extention);
using (var s = blob.OpenWrite())
{
var w = new StreamWriter(s);
w.BaseStream.Write(byteArrayData, 0, byteArrayData.Length);
w.Flush();
}

newImage.BinaryData = blob;

var imageContentRef = contentRepository.Save(newImage, SaveAction.Publish, AccessLevel.NoAccess);

Thanks in advance

#187759 Feb 02, 2018 4:07
  • Member since: 2010
     

    Hi, Jaanna,

    Browser caches based on the URL, so when uploading a new image, you need to change the URL segment if you want hint the browser that the image has changed.

    So, you could either name the image based on a date and then clear the URL segment (so that Epi automatically generates it for you) or you could just set the URL segment (but then you need to think of special characters yourself.

    I do the first thing, but you will need to adjust this code to match your variable names:

    var filename = string.Format("{0}{1}", DateTime.Now.ToString("yyyy-MM-dd_HHmm"), Path.GetExtension(file.FileName));
    
    epiImageFile.Name = filename;
    epiImageFile.RouteSegment = null;

    BR,
    Marija

    #187796 Feb 02, 2018 14:19
  • Member since: 2011
     

    @marija, you mean "renaming" the file?!

    #187815 Feb 02, 2018 19:32
  • Member since: 2010
     

    Yes, "renaming" the URL segment rather (as a consequence of renaming the Media and setting the segment to null).

    // In case the URL for the image isn't important, which is seldom the case for images.

    #187849 Feb 05, 2018 9:44