Skip to main content
Updated `.success` to `.then`
Source Link
georgeawg
  • 49k
  • 13
  • 77
  • 98
<input accept="image/*" name="file" ng-value="fileToUpload"
       value="{{fileToUpload}}" file-model="fileToUpload"
       set-file-data="fileToUpload = value;" 
       type="file" id="my_file" />
in controller:

  // code this accordingly, so that your file object 
  // will be picked up in service call below.
  fileUpload.uploadFileToUrl(file); 


in service:

  .service('fileUpload', ['$http', 'ajaxService',
    function($http, ajaxService) {

      this.uploadFileToUrl = function(data) {
        var data = {}; //file object 

        var fd = new FormData();
        fd.append('file', data.file);

        $http.post("endpoint server path to whom sending file", fd, {
            withCredentials: false,
            headers: {
              'Content-Type': undefined
            },
            transformRequest: angular.identity,
            params: {
              fd
            },
            responseType: "arraybuffer"
          })
          .successthen(function(response,) status,{
 headers, config) {         var data = response.data;
            var status = response.status;
            console.log(responsedata);

            if (status == 200 || status == 202) //do whatever in success
            else // handle error in  else if needed 
          })
          .errorcatch(function(error, status, headers, config) {
            console.log(error.status);

            // handle else calls
          });
      }
    }
  }])
<script src="https:src="//ajax.googleapisunpkg.com/ajax/libs/angularjs/1.2.23angular/angular.min.js"></script>
<input accept="image/*" name="file" ng-value="fileToUpload" value="{{fileToUpload}}" file-model="fileToUpload" set-file-data="fileToUpload = value;" type="file" id="my_file" />
in controller:

  // code this accordingly, so that your file object 
  // will be picked up in service call below.
  fileUpload.uploadFileToUrl(file); 


in service:

  .service('fileUpload', ['$http', 'ajaxService',
    function($http, ajaxService) {

      this.uploadFileToUrl = function(data) {
        var data = {}; //file object 

        var fd = new FormData();
        fd.append('file', data.file);

        $http.post("endpoint server path to whom sending file", fd, {
            withCredentials: false,
            headers: {
              'Content-Type': undefined
            },
            transformRequest: angular.identity,
            params: {
              fd
            },
            responseType: "arraybuffer"
          })
          .success(function(response, status, headers, config) {
            console.log(response);

            if (status == 200 || status == 202) //do whatever in success
            else // handle error in  else if needed 
          })
          .error(function(error, status, headers, config) {
            console.log(error);

            // handle else calls
          });
      }
    }
  }])
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<input accept="image/*" name="file" ng-value="fileToUpload"
       value="{{fileToUpload}}" file-model="fileToUpload"
       set-file-data="fileToUpload = value;" 
       type="file" id="my_file" />
in controller:

  // code this accordingly, so that your file object 
  // will be picked up in service call below.
  fileUpload.uploadFileToUrl(file); 


in service:

  .service('fileUpload', ['$http', 'ajaxService',
    function($http, ajaxService) {

      this.uploadFileToUrl = function(data) {
        var data = {}; //file object 

        var fd = new FormData();
        fd.append('file', data.file);

        $http.post("endpoint server path to whom sending file", fd, {
            withCredentials: false,
            headers: {
              'Content-Type': undefined
            },
            transformRequest: angular.identity,
            params: {
              fd
            },
            responseType: "arraybuffer"
          })
          .then(function(response) {
            var data = response.data;
            var status = response.status;
            console.log(data);

            if (status == 200 || status == 202) //do whatever in success
            else // handle error in  else if needed 
          })
          .catch(function(error) {
            console.log(error.status);

            // handle else calls
          });
      }
    }
  }])
<script src="//unpkg.com/angular/angular.js"></script>
added 18 characters in body
Source Link
Stefan Falk
  • 25.8k
  • 62
  • 223
  • 420
in controller:

  fileUpload.uploadFileToUrl(file); // code this accordingly, so that your file object 
  // will be picked up in service call below.
  fileUpload.uploadFileToUrl(file); 


in service:

  .service('fileUpload', ['$http', 'ajaxService',
    function($http, ajaxService) {

      this.uploadFileToUrl = function(data) {
        var data = {}; //file object 

        var fd = new FormData();
        fd.append('file', data.file);

        $http.post("endpoint server path to whom sending file", fd, {
            withCredentials: false,
            headers: {
              'Content-Type': undefined
            },
            transformRequest: angular.identity,
            params: {
              fd
            },
            responseType: "arraybuffer"
          })
          .success(function(response, status, headers, config) {
            console.log(response);

            if (status == 200 || status == 202) //do whatever in success
            else // handle error in  else if needed 
          })
          .error(function(error, status, headers, config) {
            console.log(error);

            // handle else calls
          });
      }
    }
  }])
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
in controller:

  fileUpload.uploadFileToUrl(file); //code this accordingly, so that your file object will be picked up in service call below.



in service:

  .service('fileUpload', ['$http', 'ajaxService',
    function($http, ajaxService) {

      this.uploadFileToUrl = function(data) {
        var data = {}; //file object 

        var fd = new FormData();
        fd.append('file', data.file);

        $http.post("endpoint server path to whom sending file", fd, {
            withCredentials: false,
            headers: {
              'Content-Type': undefined
            },
            transformRequest: angular.identity,
            params: {
              fd
            },
            responseType: "arraybuffer"
          })
          .success(function(response, status, headers, config) {
            console.log(response);

            if (status == 200 || status == 202) //do whatever in success
            else // handle error in  else if needed 
          })
          .error(function(error, status, headers, config) {
            console.log(error);

            // handle else calls
          });
      }
    }
  }])
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
in controller:

  // code this accordingly, so that your file object 
  // will be picked up in service call below.
  fileUpload.uploadFileToUrl(file); 


in service:

  .service('fileUpload', ['$http', 'ajaxService',
    function($http, ajaxService) {

      this.uploadFileToUrl = function(data) {
        var data = {}; //file object 

        var fd = new FormData();
        fd.append('file', data.file);

        $http.post("endpoint server path to whom sending file", fd, {
            withCredentials: false,
            headers: {
              'Content-Type': undefined
            },
            transformRequest: angular.identity,
            params: {
              fd
            },
            responseType: "arraybuffer"
          })
          .success(function(response, status, headers, config) {
            console.log(response);

            if (status == 200 || status == 202) //do whatever in success
            else // handle error in  else if needed 
          })
          .error(function(error, status, headers, config) {
            console.log(error);

            // handle else calls
          });
      }
    }
  }])
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
deleted 84 characters in body
Source Link
thomaux
  • 19.8k
  • 10
  • 83
  • 107

While if you are using angularjs, this is just so simple as anyone can think about it.

First of all

  1. You don't need any special changes in the structure i. I mean(html: html input tags).

While if you are using angularjs, this is just so simple as anyone can think about it.

First of all

  1. You don't need any special changes in structure i mean(html input tags)

First of all

  1. You don't need any special changes in the structure. I mean: html input tags.
Source Link
Prasad Shinde
  • 674
  • 1
  • 7
  • 15
Loading