File Upload using AngularJS

前端 未结 29 2020
野趣味
野趣味 2020-11-21 07:24

Here is my HTML form:

29条回答
  •  长发绾君心
    2020-11-21 08:09

    in simple words

    in Html - add below code only

         
      
     
    
    

    in the controller - This function is called when you click "upload file button". it will upload the file. you can console it.

    $scope.uploadedFile = function(element) {
    $scope.$apply(function($scope) {
      $scope.files = element.files;         
    });
    }
    

    add more in controllers - below code add into the function . This function is called when you click on button which is used "hitting the api (POST)". it will send file(which uploaded) and form-data to the backend .

    var url = httpURL + "/reporttojson"
            var files=$scope.files;
    
             for ( var i = 0; i < files.length; i++)
             {
                var fd = new FormData();
                 angular.forEach(files,function(file){
                 fd.append('file',file);
                 });
                 var data ={
                  msg : message,
                  sub : sub,
                  sendMail: sendMail,
                  selectUsersAcknowledge:false
                 };
    
                 fd.append("data", JSON.stringify(data));
                  $http.post(url, fd, {
                   withCredentials : false,
                   headers : {
                    'Content-Type' : undefined
                   },
                 transformRequest : angular.identity
                 }).success(function(data)
                 {
                      toastr.success("Notification sent successfully","",{timeOut: 2000});
                      $scope.removereport()
                       $timeout(function() {
                        location.reload();
                    }, 1000);
    
                 }).error(function(data)
                 {
                  toastr.success("Error in Sending Notification","",{timeOut: 2000});
                  $scope.removereport()
                 });
            }
    

    in this case .. i added below code as form data

    var data ={
              msg : message,
              sub : sub,
              sendMail: sendMail,
              selectUsersAcknowledge:false
             };
    

提交回复
热议问题