AngularJS PHP – upload and display image using FileReader with example
In this post we will give you information about AngularJS PHP – upload and display image using FileReader with example. Hear we will give you detail about AngularJS PHP – upload and display image using FileReader with exampleAnd how to use it also give you demo for it if it is necessary.
AngularJS PHP – upload and display image using FileReader with example
In this post, i am going to tell you how to upload image with preview selected image to make sure you are going to upload valid images using AngularJS and PHP.
Here in this example, you will learn how to create your own custom service for common functionality to reuse code in your controller.
I here use simple HTML for input controls and use AngularJS for posting file data on server using $http service that is a core service of AngularJS for providing facility to communicate with the remote HTTP servers.
This is very simple example to upload any images over servers and you can easily implement this code in your application.
To handle form data on server, i create a PHP file for moving files into a directory.
How to revoke 'git add' before 'git commit' ?
In this post we will give you information about How to revoke 'git add' before 'git commit' ?. Hear we will give you detail about How to revoke 'git add' before 'git commit' ?And how to use it also give you demo for it if it is necessary.
Sometimes you need to undo your git file before you git comment command. Because you made a some misteck or wrong code and you did git add using "git add ." command then you think how to undo your code before git commit command. But you can do that using git reset command, if you need to undo all file Or you may need to undo just one then you can do using git reset command, you can see bellow command.
Example:
// For Spesific filegit reset
// For all files
git reset
Hope this code and post will helped you for implement How to revoke 'git add' before 'git commit' ?. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs
So you will have to create a images directory on root first and then create two files : index.html and upload.php
index.html
- <!DOCTYPE html>
- <html>
- <head>
- <title>AngularJS PHP- upload and display image using FileReader with example</title>
- <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
- <body>
- </head>
- <body ng-app="myApp" ng-controller="ImageController">
- <input type="file" ng-model="myFile"class="form-control" accept="image/*"onchange="angular.element(this).scope().uploadedFile(this)">
- <button ng-click ="uploadFile()">upload me</button>
- <br/>
- <img ng-src="{{src}}">
- <script type="text/javascript">
- var app = angular.module('myApp',[]);
- // Here we write a custom service for upload file to reuse it in the controller
- app.service('uploadFile',['$http',function($http){
- this.uploadFiletoServer =function(file, uploadUrl){
- var fd =newFormData();
- fd.append('file', file);
- $http.post(uploadUrl, fd,{
- transformRequest: angular.identity,
- headers:{'Content-Type':undefined,'Process-Data':false}
- })
- .success(function(data){
- alert(data);
- })
- .error(function(){
- alert("Error");
- });
- }
- }]);
- app.controller('ImageController',['$scope','uploadFile',function($scope, uploadFile){
- $scope.uploadFile =function(){
- $scope.myFile = $scope.files[];
- var file = $scope.myFile;
- var url ="upload.php";
- uploadFile.uploadFiletoServer(file, url);
- };
- $scope.uploadedFile =function(element){
- var reader =newFileReader();
- reader.onload=function(event){
- $scope.$apply(function($scope){
- $scope.files = element.files;
- $scope.src = event.target.result
- });
- }
- reader.readAsDataURL(element.files[]);
- }
- }]);
- </script>
- </body>
- </html>
<!DOCTYPE html>
<html>
<head>
<title>AngularJS PHP- upload and display image using FileReader with example</title>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>
</head>
<body ng-app="myApp" ng-controller="ImageController">
<input type="file" ng-model="myFile" accept="image/*" onchange="angular.element(this).scope().uploadedFile(this)">
<button ng-click = "uploadFile()">upload me</button>
<br/>
<img ng-src="{{src}}">
<script type="text/javascript">
var app = angular.module('myApp',[]);
// Here we write a custom service for upload file to reuse it in the controller
app.service('uploadFile', ['$http', function ($http) {
this.uploadFiletoServer = function(file, uploadUrl){
var fd = new FormData();
fd.append('file', file);
$http.post(uploadUrl, fd, {
transformRequest: angular.identity,
headers: {'Content-Type': undefined,'Process-Data': false}
})
.success(function(data){
alert(data);
})
.error(function(){
alert("Error");
});
}
}]);
app.controller('ImageController', ['$scope', 'uploadFile', function($scope, uploadFile){
$scope.uploadFile = function() {
$scope.myFile = $scope.files[0];
var file = $scope.myFile;
var url = "upload.php";
uploadFile.uploadFiletoServer(file, url);
};
$scope.uploadedFile = function(element) {
var reader = new FileReader();
reader.onload = function(event) {
$scope.$apply(function($scope) {
$scope.files = element.files;
$scope.src = event.target.result
});
}
reader.readAsDataURL(element.files[0]);
}
}]);
</script>
</body>
</html>
In above code, you will see i create a service uploadFile and where i need it then i will inject this service in that controller.
upload.php
- <?php
- if(!empty($_FILES['file'])){
- $ext=pathinfo($_FILES['file']['name'],PATHINFO_EXTENSION);
- $image=time().'.'.$ext;
- move_uploaded_file($_FILES["file"]["tmp_name"],'images/'.$image);
- echo$image." successfully uploaded";
- }else{
- echo"Invalid File or Empty File";
- }
- ?>
<?php
if(!empty($_FILES['file'])){
$ext = pathinfo($_FILES['file']['name'],PATHINFO_EXTENSION);
$image = time().'.'.$ext;
move_uploaded_file($_FILES["file"]["tmp_name"], 'images/'.$image);
echo $image." successfully uploaded";
}else{
echo "Invalid File or Empty File";
}
?>
Click here to know how to submit a form in AngularJS
Hope this code and post will helped you for implement AngularJS PHP – upload and display image using FileReader with example. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs