Sunday, 19 May 2013

Starting file download with Javascript

Ever wondered how many sites triggers download after a given timeinterval or by pressing a button or clicking a link.
In this tutorial I'm going to show you how to various methods initiate downloads from your website.
  1. Simple Old Method :

    This is the simpleset method to start download, just create a <a> and point it to your file location.
     
    <a href="file.ext" > Click Here <a> To Download
    
  2. Javascript based download

    We can use a simple javascript to start file download. We will use window.location method to initiate the download.
    HTML code :
     
    <input type="button" value="Download" onClick="download('file.ext')" >
    

    JavaScript Code:
     
    <script>
    function download(file)
    {
     window.location=file;
    }
    </script>
    
  3. Download using javascript and Iframe

    <iframe id="frame" style="display:none"></iframe>
    <a href="javascript:download('file.ext')">download</a>
    
    <script type="text/javascript">
    function download(path) 
    {
        var ifrm = document.getElementById("frame");
        ifrm.src = path;
    }
    </script>
    
  4. Download using Javascript, Iframe and PHP

    Create a Link :
    <iframe id="frame" style="display:none"></iframe>
    <a href="javascript:download('file.ext')">download</a>
    
    Javascript:
    <script type="text/javascript">
    function download(path) 
    {
        var ifrm = document.getElementById(frame);
        ifrm.src = "download.php?path="+path;
    }
    </script>
    
    Code for download.php file :
    <?php 
       header("Content-Type: application/octet-stream");
       header("Content-Disposition: attachment; filename=".$_GET['path']);
       readfile($_GET['path']);
    ?>
    
  5. Javascript based download with timer

    Above method can be used to start a javascript based timer and at the end of timer download will start.
    Simply put the code in file and it will start the timer.
    After time is up we can either display link or directly start download.
    You can use all of above method with the timer with few changes. HTML code:
     
    <div id="counter-text"> Please Wait For <span id="counter">  </span> Seconds. 
    </div>
    <a href="javascript:download('file.ext')" style="display:none" id="link"> 
    Click To Download </a>
    
    JavaScript Code:
     
    <script>
    time = 5;
    document.getElementById("counter").innerHTML = time;
    timer = setInterval("count()",1000);
    
    function count()
    {
     if(time == 1)
     {
      //Cleare Timer and remove counter from page
      clearInterval(timer);
      document.getElementById("counter-text").style.display="none";
      //If you want to display link
      document.getElementById("link").style.display="block";
      //Or directly start download
      //download('file.ext');
     }
     else
     {
      time--;
      document.getElementById("counter").innerHTML = time;
     }
    }
    function download(file)
    {
     window.location=file;
    }
    </script>
    
Ads :
Buy Apple Iphone 5, iphone 4S On www.ebay.com
Electronics, Cars, Fashion, Collectibles, Coupons and More Online Shopping | eBay
www.ebay.co.uk | www.ebay.com.my

Related Posts :

Friday, 17 May 2013

PHP MySQLI Tutorial

What is MySQLI :

The MySQLi Extension (MySQL Improved) is a relational database driver used in the PHP programming language to provide an interface with MySQL databases.
MySQLi is an improved version of the older PHP MySQL driver, offering various benefits.
The developers of the PHP programming language recommend using MySQLi when dealing with MySQL server versions 4.1.3 and newer (takes advantage of new functionality).

Why Mysqli :

The MySQLi extension provides various benefits with respect to its predecessor, the most prominent of which are:
  • An object-oriented interface
  • Support for prepared statements
  • Support for multiple statements
  • Support for transactions
  • Enhanced debugging support
  • Embedded server support

MySQLi Vs MySQL

MySQLi MySQL
PHP version introduced 5.0 Prior to 3.0
Included with PHP 5.x yes Yes
MySQL development status Active development Maintenance only
Recommended by MySQL for new projects Yes - preferred option No
API supports Charsets Yes No
API supports server-side Prepared Statements Yes No
API supports Stored Procedures Yes No
API supports Multiple Statements Yes No
Supports all MySQL 4.1+ functionality Yes No


Here is a simple code that implements mysqli

<?php


 //CONNECT TO THE DATABASE
 $DB_HOST = 'HOST';
 $DB_USER = 'USERNAME';
 $DB_PASS = 'PASSWORD';
 $DB_NAME = "mysqlitest";

 $mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
 
 
 if (mysqli_connect_errno()) {
  printf("Connect failed: %s\n", mysqli_connect_error());
  exit();
 }

 //CREATE TABLE
 $q = "CREATE TABLE user (id int AUTO_INCREMENT PRIMARY KEY, 
     name varchar(30))";
 $mysqli->query($q);
 
 //INSERT FEW VALUES
 $q = "INSERT INTO user VALUES (NULL, 'abc')";
 $mysqli->query($q);
 $q = "INSERT INTO user VALUES (NULL, 'bcd')";
 $mysqli->query($q);
 $q = "INSERT INTO user VALUES (NULL, 'xyz')";
 $mysqli->query($q);
 
 
 //SELECT A USER
 $q = "SELECT * FROM user WHERE id=3";
 $result = $mysqli->query($q) or die($mysqli->error.__LINE__);

 // GOING THROUGH THE DATA
 if($result->num_rows > 0) {
  while($row = $result->fetch_assoc()) {
   echo $row['name']; 
  }
 }
 else {
  echo 'NO RESULTS'; 
 }
 // CLOSE CONNECTION
 mysqli_close($mysqli);
?>

Ads :
Buy iPad Tablet eBook Reader on www.ebay.com
Electronics, Cars, Fashion, Collectibles, Coupons and More Online Shopping | eBay
www.ebay.co.uk | www.ebay.com.my

Wednesday, 15 May 2013

How to upload file to server in android


If you are building an app that requires files to be uploaded on server you will need to follow this method. By using this method,  you will be able to upload a file from your SD Card.

We will be using httpmime-4.2.5.jar for this tutorial. You can download it from here. Extract the zip file and you will find jar file under lib folder.


  1. Create a new android project in Eclipse, and add the jar file to lib folder.
  2. Add android.permission.INTERNET and android.permission.WRITE_EXTERNAL_STORAGE in your manifest file.
  3. Use the following code :
 String address;
 MultipartEntity entity;
 File f;
 FileBody fb;
 entity = new MultipartEntity(
    HttpMultipartMode.BROWSER_COMPATIBLE);
 address = Environment.getExternalStorageDirectory()
    + "/temp.dat";
 f = new File(address);
 fb = new FileBody(f, "application/octect-stream");
 entity.addPart("file", fb);
 HttpClient httpclient = new DefaultHttpClient();
 HttpPost httppost = new HttpPost("Your URL Here");
 
 httppost.setEntity(entity);
 HttpResponse response = httpclient.execute(httppost);
 BufferedReader bufferedReader = new BufferedReader(
  new InputStreamReader(
   response.getEntity().getContent()));
 StringBuffer stringBuffer = new StringBuffer("");
 String line = "";
 String LineSeparator = System.getProperty("line.separator");
 while ((line = bufferedReader.readLine()) != null) {
  stringBuffer.append(line + LineSeparator);
 }
 bufferedReader.close();
 return stringBuffer.toString();
 } catch (ClientProtocolException e) {
  return e.toString();
 } catch (IOException e) {
  return e.toString();
 } 
Here we use MultipartEntity from the http library to add file for uploading.
f = new File(address);
fb = new FileBody(f, "application/octect-stream");
entity.addPart("file", fb);
We select the file given by address and then we get filebody using the FileBody Class and then add it to the entity.
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("Your URL Here");
httppost.setEntity(entity);
HttpResponse response = httpclient.execute(httppost);
We create a new HttpClient and a HttpPost object, set entity and execute the reuqest.
Now android does not allow us to use network operations under the main thread, either we have to create a new thread or we use AsyncTask. You can Find the full source code here or download it here .
You can also use FTP library to uplaod the file which is better than this approach as described here.
Ads :
Buy Hard Drive, RAM, CD, DVD, USB on www.ebay.com
Electronics, Cars, Fashion, Collectibles, Coupons and More Online Shopping | eBay
www.ebay.co.uk | www.ebay.com.my

Tuesday, 4 December 2012

Some useful shortcut keys for windows

We often use the mouse to do many things on our pc or laptop. Using mouse is easy but some time slow in accessing some functions. So, we have some shortcut key for many functions that windows provides.
Here is the list :
ctrl + w Closes the current window
ctrl + e
ctrl + f
Reach to the search box (Windows 7)
ctrl + r Refresh
ctrl + n Opens new copy of same window
alt + p Toggle show file preview
alt + d Direct access to address bar
alt + tab Switch windows
alt + space Open command menu
window key + up arrow Maximizes window
window key + down arrow If window is maximized, restores it
else minimizes it
window key + left arrow Move window to left
window key + right arrow Move window to right
window key + e Opens explorer
window key + d Shows desktop
window key + f Open new search window
window key + l Switch user
window key + f Open new search window
window key + x Opens windows mobility center
window key + b reach to notification area
(right side on taskbar)
window key + m minimize current window
window key + tab Switch windows
ctrl + shift + esc Open task manager
ctrl + esc / Window key Open start menu
ctrl + shift + n New folder
windows + number key Open nth item from task bar
(Window+1 will open 1st item)

Ads :
Buy Laptops and netbooks : HP, DELL, SONY, ASUS on www.ebay.com
Electronics, Cars, Fashion, Collectibles, Coupons and More Online Shopping | eBay
www.ebay.co.uk | www.ebay.com.my

Wednesday, 19 September 2012

Sorting With Sound Effects

Sorting, huh.. whenever you heard about, it is always a boring topic. But what about adding a little sound effect to it.
Here are some videos showing how these algorithms work with a little sound effect. Instead of values for data sound peach is used.







And What about explaining insertion sort in form of a folk song :)

Hey, if you liked it, please click on ad below...


Ads :
Buy Apple MAC, iMAC, MAC mini on www.ebay.com
Electronics, Cars, Fashion, Collectibles, Coupons and More Online Shopping | eBay
www.ebay.co.uk | www.ebay.com.my

Monday, 17 September 2012

How to change Facebook default theme

Have you ever thought how good it would be if you could change default theme of Facebook. Yes, now you can do this, and do it very easily.
You Just have to use the script provided below..
Instructions :
If you are using IE then just open Facebook and copy paste the script in address bar and press enter.
If you are using Firefox Create a bookmark button on bookmark bar with the script as loaction (To add bookmark to bookmark toolbar goto bookmarks->bookmarks Toolbar). Now open Facebook and click on bookmarked button. If you cant see bookmarks toolbar select View->toolbars->bookmarks Toolbar. This is just a test version that changes all the colors.
NOTE : you can enter name of color or hex code in prompt box. If you like it please click on the ad below.

javascript:change();
function change()
{
    var x = document.getElementsByTagName('title');
    x[0].innerHTML =  "ZZ Script | "+x[0].innerHTML;
    var col = prompt("Enter Color");
    x = document.getElementsByTagName('div');
    var i=0;
    while(i < x.length)
    {
 x[i].style.backgroundColor=col;
 x[i].style.borderColor=col;
        i++;
    }

    x = document.getElementsByTagName('li');
    var i=0;
    while(i < x.length)
    {
 x[i].style.backgroundColor=col;
 x[i].style.borderColor=col;
        i++;
    }
    x = document.getElementsByTagName('ul');
    var i=0;
    while(i < x.length)
    {
 x[i].style.backgroundColor=col;
 x[i].style.borderColor=col;
        i++;
    }
    x = document.getElementsByTagName('a');
    var i=0;
    while(i < x.length)
    {
 x[i].style.backgroundColor=col;
 x[i].style.borderColor=col;
        i++;
    }
    x = document.getElementsByTagName('span');
    var i=0;
    while(i < x.length)
    {
 x[i].style.backgroundColor=col;
 x[i].style.borderColor=col;
        i++;
    }
    x = document.getElementsByTagName('input');
    var i=0;
    while(i < x.length)
    {
 x[i].style.backgroundColor=col;
 x[i].style.borderColor=col;
        i++;
    }
}
Ads :
Buy Amazon Kindle Fire, Microsoft Surface, Apple Ipad on www.ebay.com
Electronics, Cars, Fashion, Collectibles, Coupons and More Online Shopping | eBay
www.ebay.co.uk | www.ebay.com.my

Sunday, 16 September 2012

How to play Nintendo games on your computer



Ever wondered how to play this old Nintendo games on your computer !!
Yes, you can play these games on your computer using emulators. An emulator is hardware or software or both that duplicates (or emulates) the functions of a first computer system (the guest) in a different second computer system (the host), so that the emulated behavior closely resembles the behavior of the real system. (Wikipedia)
So to play these old games you can use different emulator. One of them is jnes (links at bottom of this post).
Once you have downloaded jnes extract it and run the executable file. From file browsers nevigate to games folder emulator directory select super_mario_bros.nes file and the game will start. goto options->input and change the keyboard setting. If you want to play more games google for .nes file for your game. Not only Nintendo you can also use emulators for PSP, PS-II, PS-I, XBOX, etc.
Here are some links from where you can download .nes files :
www.freeroms.com
www.nesfiles.com
www.romhustler.net
Download Jnes

Click here



Ads :
Buy Smart Watches on www.ebay.com
Electronics, Cars, Fashion, Collectibles, Coupons and More Online Shopping | eBay
www.ebay.co.uk | www.ebay.com.my