Popular Posts

Monday, October 31, 2011

Download images

public static void downloadFile(System.Web.UI.Page pg, string filepath)
    {
        pg.Response.AppendHeader("content-disposition", "attachment; filename=" + new FileInfo(filepath).Name);
        pg.Response.ContentType = getContentType(new FileInfo(filepath).Extension);
        pg.Response.WriteFile(filepath);
        pg.Response.End();
    }



   public static string getContentType(string Fileext)
    {

        string contenttype = "";
        switch (Fileext)
        {
            case ".xls":
                contenttype = "application/vnd.ms-excel";
                break;
            case ".doc":
                contenttype = "application/msword";
                break;
            case ".ppt":
                contenttype = "application/vnd.ms-powerpoint";
                break;
            case ".pdf":
                contenttype = "application/pdf";
                break;
            case ".jpg":
            case ".jpeg":
                contenttype = "image/jpeg";
                break;
            case ".gif":
                contenttype = "image/gif";
                break;
            case ".ico":
                contenttype = "image/vnd.microsoft.icon";
                break;
            case ".zip":
                contenttype = "application/zip";
                break;
            default: contenttype = "";
                break;
        }
        return contenttype;
    }

calling:


downloadFile(this.Page, Server.MapPath("~/images/layout_web.jpg"));

Random value/Image from Javascript Array



For load Random Images

var arrName = new Array(2);
  arrName[0] = "http://abc.com/1.png";
  arrName[1] = " http://abc.com/2.png";

for link on Images

var arrLink = new Array(2);
  arrLink[0] = "http://abc.com/xyz.aspx?id=1";
  arrLink[1] = " http://abc.com/xyz.aspx?id=2";


  var firstRandomNumber = (Math.round((Math.random()*14)+1))

Load Randon Image and Link.

  document.write("<a href="+arrLink[firstRandomNumber]   +"> "+"<img src= "  + arrName[firstRandomNumber]+">");