AngularJS PHP - upload and display image using FileReader with example

AngularJS PHP - upload and display image using FileReader with example

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.

So you will have to create a images directory on root first and then create two files : index.html and upload.php

index.html
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>AngularJS PHP- upload and display image using FileReader with example</title>
  5. <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
  6. <body>
  7. </head>
  8. <body ng-app="myApp" ng-controller="ImageController">
  9.     <input type="file" ng-model="myFile" class="form-control" accept="image/*" onchange="angular.element(this).scope().uploadedFile(this)">
  10.     <button ng-click = "uploadFile()">upload me</button>
  11.     <br/>
  12.     <img ng-src="{{src}}">
  13. <script type="text/javascript">
  14.      var app = angular.module('myApp',[]);
  15.      // Here we write a custom service for upload file to reuse it in the controller
  16.         app.service('uploadFile', ['$http', function ($http) {
  17.          this.uploadFiletoServer = function(file, uploadUrl){
  18.          var fd = new FormData();
  19.          fd.append('file', file);
  20.          $http.post(uploadUrl, fd, {
  21.          transformRequest: angular.identity,
  22.          headers: {'Content-Type': undefined,'Process-Data': false}
  23.          })
  24.          .success(function(data){
  25.          alert(data);
  26.          })
  27.          .error(function(){
  28.          alert("Error");
  29.          });
  30.          }
  31.          }]);
  32. app.controller('ImageController', ['$scope', 'uploadFile', function($scope, uploadFile){
  33.      $scope.uploadFile = function() {
  34.          $scope.myFile = $scope.files[0];
  35.             var file = $scope.myFile;
  36.      var url = "upload.php";
  37.      uploadFile.uploadFiletoServer(file, url);
  38.      };
  39.      $scope.uploadedFile = function(element) {
  40.          var reader = new FileReader();
  41.          reader.onload = function(event) {
  42.          $scope.$apply(function($scope) {
  43.          $scope.files = element.files;
  44.              $scope.src = event.target.result
  45.          });
  46.          }
  47. reader.readAsDataURL(element.files[0]);
  48.          }
  49.     }]);
  50. </script>
  51. </body>
  52. </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
  1. <?php
  2.     if(!empty($_FILES['file'])){
  3.         $ext = pathinfo($_FILES['file']['name'],PATHINFO_EXTENSION);
  4. $image = time().'.'.$ext;
  5. move_uploaded_file($_FILES["file"]["tmp_name"], 'images/'.$image);
  6.         echo $image." successfully uploaded";
  7.     }else{
  8.         echo "Invalid File or Empty File";
  9.     }
  10. ?>

Phone: (+91) 8800417876
Noida, 201301
ataşehir escort maltepe escort bostancı escort kadıköy escort maltepe escort ataşehir escort tuzla escort pendik escort ümraniye escort kartal escort
pendik escort
izmir escort izmir escort izmir escort izmir escort denizli escort antalya escort antalya escort antalya escort izmir escort izmir escort izmir escort izmir escort izmir escort
hatay escort nevsehir escort nigde escort kirikkale escort batman escort osmaniye escort kirklareli escort tokat escort rize escort adiyaman escort gumushane escort batman escort
porno izle
cialis satış cialis fiyat cialis 20 mg cialis 100 mg cialis 5 mg fiyat
sohbet okey oyna
ankara escort marmaris escort bursa escort mersin escort antalya escort izmir escort samsun escort bodrum escort çeşme escort kuşadası escort adana escort eskişehir escort gaziantep escort esenyurt escort pendik escort tuzla escort fatih escort ümraniye escort mecidiyeköy escort kartal escort bahçelievler escort bahçeşehir escort ataşehir escort bakırköy escort nişantaşı escort avcılar escort büyükçekmece escort kurtkoy escort konya escort beşiktaş escort taksim escort beylikdüzü escort malatya escort sakarya escort şişli escort maltepe escort kadıköy escort ataköy escort bostancı escort şirinevler escort bağcılar escort üsküdar escort etiler escort başaksehir escort ortaköy escort kağıthane escort sultangazi escort halkalı escort çekmeköy escort şaşkınbakkal escort mamak escort sincan escort keçiören escort çankaya escort beypazarı escort akyurt escort pursaklar escort kahramankazan escort göztepe escort alsancak escort bornova escort buca escort karabaglar escort karşıyaka escort konak escort narlıdere escort izmit escort başiskele escort kandira escort körfez escort karamürsel escort dilovası escort gölcük escort gebze escort kocaeli escort kayseri escort elazığ escort denizli escort diyarbakir escort adana escort ankara escort izmir escort antalya escort gaziantep escort kayseri escort konya escort mersin escort denizli escort kocaeli escort diyarbakır escort