Ajax Php File upload with Progress Bar

Ajax Php File upload with Progress Bar
 

Ajax Php File upload with Progress Bar

In this programming lesson you can learn to use HTML5 + JavaScript + PHP to render an elegant file upload progress bar in your file upload forms. It is desirable to render a file upload progress bar when users upload files to your server, much like the way YouTube renders a file upload progress bar every time we upload a video to their network. The new HTML5 progress element has been made specifically for developers to use in scenarios such as this, it automatically matches the specific browser software visually, but you can customize your loading bar graphics for branding or consistency in all the different browsers. Due to the fact that we are using a new HTML5 element, this application may not work as intended in old versions of browser software. Use a modern popular browser software when experimenting with this code.

upload_form.html
<!DOCTYPE html> <html> <head> <script> function _(el){ return document.getElementById(el); } function uploadFile(){ var file = _("file1").files[0]; var formdata = new FormData(); formdata.append("file1", file); var ajax = new XMLHttpRequest(); ajax.upload.addEventListener("progress", progressHandler, false); ajax.addEventListener("load", completeHandler, false); ajax.addEventListener("error", errorHandler, false); ajax.addEventListener("abort", abortHandler, false); ajax.open("POST", "file_upload_parser.php"); ajax.send(formdata); } function progressHandler(event){ _("loaded_n_total").innerHTML = "Uploaded "+event.loaded+" bytes of "+event.total; var percent = (event.loaded / event.total) * 100; _("progressBar").value = Math.round(percent); _("status").innerHTML = Math.round(percent)+"% uploaded... please wait"; } function completeHandler(event){ _("status").innerHTML = event.target.responseText; _("progressBar").value = 0; } function errorHandler(event){ _("status").innerHTML = "Upload Failed"; } function abortHandler(event){ _("status").innerHTML = "Upload Aborted"; } </script> </head> <body> <h2>HTML5 File Upload Progress Bar Tutorial</h2> <form id="upload_form" enctype="multipart/form-data" method="post"> <input type="file" name="file1" id="file1"><br> <input type="button" value="Upload File" onclick="uploadFile()"> <progress id="progressBar" value="0" max="100" style="width:300px;"></progress> <h3 id="status"></h3> <p id="loaded_n_total"></p> </form> </body> </html>

file_upload_parser.php
<?php $fileName = $_FILES["file1"]["name"]; $fileTmpLoc = $_FILES["file1"]["tmp_name"]; $fileType = $_FILES["file1"]["type"]; $fileSize = $_FILES["file1"]["size"]; $fileErrorMsg = $_FILES["file1"]["error"]; if (!$fileTmpLoc) { echo "ERROR: Please browse for a file before clicking the upload button."; exit(); } if(move_uploaded_file($fileTmpLoc, "test_uploads/$fileName")){ echo "$fileName upload is complete"; } else { echo "move_uploaded_file function failed"; } ?>

Spring Constructor Injection Example

Spring Constructor Injection Example

Spring Constructor Injection Example

In this example we have explained how to achieve DI using constructor injection. This example is same as previous setter injection example in term of project structure, jars required and Output but differ in Pojo’s and spring-config.xml.
Employee.java :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.poosan;
public class Employee {
    private String name;
    private int age;
    private Address address;
    public Employee(String name, int age, Address address) {
        this.name = name;
        this.age = age;
        this.address = address;
    }
    public Address getAddress() {
        return address;
    }
    @Override
    public String toString() {
        return "Name :"+name+"\n"+"Age :"+age;
    }
}

Address.java :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.thecafetechno;
public class Address {
    private String city;
    private String state;
    public Address(String city,String state) {
        this.city=city;
        this.state=state;
    }
    @Override
    public String toString() {
        return "City: "+city+"\n"+"State: "+state;
    }
}

EmployeeAddress.java :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.poosan;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public class EmployeeAddress {
    public static void main(String[] args) {
        ClassPathResource resource = new ClassPathResource("spring-  config.xml");
        BeanFactory factory = new XmlBeanFactory(resource);
        Employee employee=(Employee)factory.getBean("employeeBean");
        System.out.println("Employee: "+employee);
        System.out.println("Address: "+employee.getAddress());
    }
}

spring-config.xml :

We have defined two beans in the spring xml configuration file. The root tag of file must be <beans>. All other beans should be declared in <beans> tag. Each bean is declared by tag <bean> inside the root tag. Attribute id is used as unique identifier which is used to access the bean. Attribute class defines the full qualified name of the class whose bean is being created.
Tag <constructor-arg> is used inside <bean> to define arguments of constructor. Constructor args are identified either by ‘type’ or ‘index’ attribute of tag <constructor-arg>. We have used both in our example to show how to use these.
In case of <constructor-arg> with ‘index’ attribute, index values start from 0, index value must match with the position of constructor arg of Pojo e.g. index=”0” for city and index=”1” for state. Order in which <constructor-arg> are defined does not matter if attribute ‘index’ is used.
In case of <constructor-arg> with ‘type’ attribute, the value of attribute ‘value’ must be compatible with type declared by attribute ‘type’. And type should be same as the type of argument of constructor. Order in which <constructor-arg> are defined does not matter if types declared by ‘type’ attribute are different int the bean as in this example types string,int and Address are different data types. But if in a bean two or more <constructor-arg> have the same type then order does matter and values are assigned to the constructor arguments in the order in which these are defined.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="employeeBean" class="com.poosan.Employee">
          <constructor-arg type="java.lang.String" value="Davinder"/>
          <constructor-arg type="int" value="24"/>
          
          <constructor-arg type="Address" ref="addressBean"/>
        </bean>
        <bean id="addressBean" class="com.poosan.Address">
         <constructor-arg index="0" value="Hoshiarpur"/>
         <constructor-arg index="1" value="Punjab"/>
        </bean>
</beans>
To run this example download code , import it into eclipse and configure build path by adding jars to classpath.