Monday 24 June 2013

Checking the uploading image size without submitting the form / Posting the upload file details without submitting the form


html:
--------

<form id="upload_form" >
      <input type="file" class="file" name="filename[]"  onchange="validateFileSize()" />
 </form>


js:
----


function validateFileSize(){
    var form_obj = document.getElementById("upload_form");  //upload_form is a form id
        try{
            var formData = new FormData(form_obj);   
            $.ajax({
                url: 'upload.php',  //server script to process data
                type: 'POST',
                success: function(data){},
                error: function(data){},
                data: formData,
                cache: false,
                contentType: false,
                processData: false
            });
        }catch(e){
        }
 }   



upload.php:
-----------------


  if (isset($_FILES["filename"]["name"])) {
    try {          
           echo "Upload: " . $_FILES["filename"]["name"] . "<br />";
            echo "Type: " . $_FILES["filename"]["type"] . "<br />";
            echo "Size: " . ($_FILES["filename"]["size"] / 1024) . " Kb<br />";
            echo "Temp file: " . $_FILES["filename"]["tmp_name"] . "<br />";
          } catch (Exception $e) {
             print_r($e);
          }    
    }








Note : FormData will not work in old browsers and IE
 For old browser, we can use iframe

http://stackoverflow.com/questions/166221/how-can-i-upload-files-asynchronously-with-jquery
http://stackoverflow.com/questions/9516251/upload-files-without-refresh-page   ===> using iframe
   
  

To Override document.write / alternative solution for document.write using javascript



<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
hai
<div id="div"></div>

<script>
document.write = function(w) {
    document.getElementById("div").innerHTML += w;    //w has the write statement from document.write
}

document.write("Hello");
document.write("world");

</script>
</body>
</html>

Getting ViewPort / Device Screen Size using Javascript (subtracting from browser scrollbar size):


function getViewport() {

    var viewPortWidth;
    var viewPortHeight;

    // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
    if (typeof window.innerWidth != 'undefined') {
      viewPortWidth = window.innerWidth,
      viewPortHeight = window.innerHeight
    }

   // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
    else if (typeof document.documentElement != 'undefined'
    && typeof document.documentElement.clientWidth !=
    'undefined' && document.documentElement.clientWidth != 0) {
       viewPortWidth = document.documentElement.clientWidth,
       viewPortHeight = document.documentElement.clientHeight
    }

    // older versions of IE
    else {
      viewPortWidth = document.getElementsByTagName('body')[0].clientWidth,
      viewPortHeight = document.getElementsByTagName('body')[0].clientHeight
    }

   if(window.orientation != undefined){
      if(window.screen){
          var deviceWidth = window.screen.width;
          var deviceHeight = window.screen.height;
      }else {
          var deviceWidth = viewPortWidth;
          var deviceHeight = viewPortHeight;
      }   
      viewPortWidth = window.orientation == 0 ? deviceWidth : deviceHeight;
      viewPortHeight = window.orientation == 0 ? deviceHeight : deviceWidth ;
      if (navigator.userAgent.indexOf('Android') >= 0 && window.devicePixelRatio) {     
          viewPortWidth = viewPortWidth/window.devicePixelRatio;
          viewPortHeight = viewPortHeight/window.devicePixelRatio;
      }
   }
  
   viewPortWidth = viewPortWidth - getScrollbarWidth();

   return viewPortWidth+'x'+ viewPortHeight;
 }

Getting the browser scrollbar width:
----------------------------------------------------------

function getScrollbarWidth() {
    var outer = document.createElement("div");
    outer.style.visibility = "hidden";
    outer.style.width = "100px";
    document.body.appendChild(outer);
   
    var widthNoScroll = outer.offsetWidth;
    // force scrollbars
    outer.style.overflow = "scroll";
   
    // add innerdiv
    var inner = document.createElement("div");
    inner.style.width = "100%";
    outer.appendChild(inner);       
   
    var widthWithScroll = inner.offsetWidth;
   
    // remove divs
    outer.parentNode.removeChild(outer);
   
    return widthNoScroll - widthWithScroll;
}


alert(getViewport());


Note : subtracting the scrollbar width from the screen width will give absolute screen width

To find your absolute screen size  refer url : http://responsejs.com/labs/dimensions/