如何使用AngularJS POST二进制文件(带有上载DEMO) [英] How to POST binary files with AngularJS (with upload DEMO)

查看:189
本文介绍了如何使用AngularJS POST二进制文件(带有上载DEMO)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过带有角度1的离子1发布带有某些数据的.mp4文件.通过POSTMAN发布时,它可以正常工作.我的应用程序中显示Success = false.

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.

在POSTMAN中,没有标题和数据, 带有POST请求的服务网址 http://services.example.com/upload.php 表单数据中的正文

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

在我的应用中:

$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) {

    });
}

推荐答案

推荐:直接发布二进制文件

使用multi-part/form-data发布二进制文件的效率很低,因为 base64编码会增加33 % 高架.如果服务器API接受带有二进制数据的POST,请直接发布文件:

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;
    });
}

通常 $ http服务将JavaScript对象编码为 JSON字符串.使用transformRequest: []覆盖默认转换.

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

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>

使用 FormData API 发布数据时,它会将内容类型设置为undefined很重要:

When posting data using the FormData API, it is important to set the content type to undefined:

function uploadQuestion(file) {

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

    return $http({
        method: 'POST',
        headers: { 'Content-Type': undefined ̶'̶m̶u̶l̶t̶i̶p̶a̶r̶t̶/̶f̶o̶r̶m̶-̶d̶a̶t̶a̶'̶ }, // also tried with application/x-www-form-urlencoded
        url: 'http://services.example.com/upload.php',
        data: form,
        ̶c̶a̶c̶h̶e̶:̶ ̶f̶a̶l̶s̶e̶,̶ 
        timeout: 300000
    ̶}̶)̶.̶s̶u̶c̶c̶e̶s̶s̶(̶f̶u̶n̶c̶t̶i̶o̶n̶ ̶(̶d̶a̶t̶a̶,̶ ̶s̶t̶a̶t̶u̶s̶,̶ ̶h̶e̶a̶d̶e̶r̶s̶,̶ ̶c̶o̶n̶f̶i̶g̶)̶ ̶{̶
    }).then(function(response) {
        var data = response.data;
        var status = response.status;
        if (status == '200') {
           console.log("Success");
        }    
    ̶}̶)̶.̶e̶r̶r̶o̶r̶(̶f̶u̶n̶c̶t̶i̶o̶n̶ ̶(̶d̶a̶t̶a̶,̶ ̶s̶t̶a̶t̶u̶s̶,̶ ̶h̶e̶a̶d̶e̶r̶s̶,̶ ̶c̶o̶n̶f̶i̶g̶)̶ ̶{̶
    }).catch(function(response) {
        console.log("ERROR");
        //IMPORTANT
        throw response;    
    });
}

XHR API发送方法发送时 FormData对象,它会自动设置带有适当的边界.当 $ http服务覆盖内容类型时,服务器将获得一个没有适当边界的内容类型标头.

When the XHR API send method sends a FormData Object, it automatically sets the content type header with the appropriate boundary. When the $http service overrides the content type, the server will get a content type header without the proper boundary.

这篇关于如何使用AngularJS POST二进制文件(带有上载DEMO)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆