Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
467 views
in Technique[技术] by (71.8m points)

angular http - How to POST binary files with AngularJS (with upload DEMO)

unable to send file with angular post call

I am trying to post .mp4 file with some data through ionic 1 with angular 1. While posting through POSTMAN it is fine and working. I am getting Success = false in my application.

in POSTMAN, no headers and data is bellow, Service url with POST request http://services.example.com/upload.php body in form data

j_id = 4124, type = text   
q_id = 6, type = text   
u_id = 159931, type = text 
file = demo.mp4, type = file

in my app:

$rootScope.uploadQuestion = function () {

    var form = new FormData();
    form.append("j_id", "4124");
    form.append("q_id", "6");
    form.append("u_id", "159931");
    form.append("file", $rootScope.videoAns.name); //this returns media object which contain all details of recorded video

    return $http({
        method: 'POST',
        headers: { 'Content-Type': 'multipart/form-data' }, // also tried with application/x-www-form-urlencoded
        url: 'http://services.example.com/upload.php',
        // url: 'http://services.example.com/upload.php?j_id=4124&q_id=8&u_id=159931&file='+$rootScope.videoAns.fullPath,
        // data: "j_id=" + encodeURIComponent(4124) + "&q_id=" + encodeURIComponent(8) + "&u_id=" + encodeURIComponent(159931) +"&file=" + encodeURIComponent($rootScope.videoAns), 
        data: form,
        cache: false,
        timeout: 300000
    }).success(function (data, status, headers, config) {
        if (status == '200') {
            if (data.success == "true") {
                alert('uploading...');
            }


        }


    }).error(function (data, status, headers, config) {

    });
}
Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

RECOMMENDED: POST Binary Files Directly

Posting binary files with multi-part/form-data is inefficient as the base64 encoding adds an extra 33% overhead. If the server API accepts POSTs with binary data, post the file directly:

function upload(url, file) {
    if (file.constructor.name != "File") {
       throw new Error("Not a file");
    }
    var config = {
        headers: {'Content-Type': undefined},
        transformRequest: []
    };
    return $http.post(url, file, config)
      .then(function (response) {
        console.log("success!");
        return response;
    }).catch(function (errorResponse) {
        console.error("error!");
        throw errorResponse;
    });
}

Normally the $http service encodes JavaScript objects as JSON strings. Use transformRequest: [] to override the default transformation.


DEMO of Direct POST

angular.module("app",[])
.directive("selectNgFiles", function() {
  return {
    require: "ngModel",
    link: postLink
  };
  function postLink(scope, elem, attrs, ngModel) {
    elem.on("change", function(event) {
      ngModel.$setViewValue(elem[0].files);
    });
  }
})
.controller("ctrl", function($scope, $http) {
  var url = "//httpbin.org/post";
  var config = {
    headers: { 'Content-type': undefined }
  };
  $scope.upload = function(files) {
    var promise = $http.post(url,files[0],config);
    promise.then(function(response){
      $scope.result="Success "+response.status;
    }).catch(function(errorResponse) {
      $scope.result="Error "+errorRespone.status;
    });
  };
})
<script src="//unpkg.com/angular/angular.js"></script>
  <body ng-app="app" ng-controller="ctrl">
    <input type="file" select-ng-files ng-model="files">
    <br>
    <button ng-disabled="!files" ng-click="upload(files)">
      Upload file
    </button>
    <pre>
    Name={{files[0].name}}
    Type={{files[0].type}}
    RESULT={{result}}
    </pre>
  </body>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...