Pages Menu
TwitterRssFacebook
Categories Menu

Posted by on Dec 20, 2012 in Blog, Programming | 1 comment

Force an image to load from server (PHP or ASP.NET)

prod-icon-loadbalance

I ran across a need to make sure an image on a webpage loaded from the server on each load of the page.  This was accomplished by loading the images with a random number as a parameter.
So it ended up looking like:  <img src=”http://www.website.com/images/1.png?rndNum=2204>

In PHP this was done by adding:  ?<?php echo rand(1000,7000)?> to the image source.

In asp.net (VB):  Using an asp:image with id =”Image1″ on the page.
VB code behind:
If Not Me.IsPostBack Then
Dim randImgNum As Integer
randImgNum = RandomNumber(1000,7000)
Me.Image1.ImageUrl = “http://www.website.com/1.png?rndNum=” & randImgNun
end if

 

 

1 Comment

  1. Nicely done. I am more prone to name images with some meaning, like category, user ID and image upload order. In vb.net that looks like this:
    Dim ImgExt1 As String = LCase(System.IO.Path.GetExtension(ImgUpload1.FileName))
    Dim ImgName1 = “gen” & aID.ToString & “_” & uID.ToString & “_1″ & ImgExt1
    Dim ImgExt2 As String = LCase(System.IO.Path.GetExtension(ImgUpload2.FileName))
    Dim ImgName2 = “gen” & aID.ToString & “_” & uID.ToString & “_2″ & ImgExt2, etc ….
    where:
    - gen is abbreviation for general category
    - aID is article name or number
    - uID is user ID
    - _1 or _2 is number of the image.

    This is general rule when adding new article and images associates with it. When editing this article I generally use the same rule so the current images gets replaced with newly uploaded image. No additional image is related to the same article and user is added in image repository.

    Just an idea.

Post a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>