AngularJS-如何读取图像文件';谁的数据?

AngularJS-如何读取图像文件';谁的数据?,angularjs,angular-directive,Angularjs,Angular Directive,我想把图像数据发送到后端。为此,我尝试读取文件(图像)。但我发现了一个错误: Uncaught TypeError: Failed to execute 'readAsArrayBuffer' on 'FileReader': parameter 1 is not of type 'Blob'. 如何修复它。或者,读取图像文件的正确方法是什么? 这是我的“指令” var userPhotoUpload = function () { return { link :

我想把
图像
数据发送到后端。为此,我尝试读取文件(图像)。但我发现了一个错误:

Uncaught TypeError: Failed to execute 'readAsArrayBuffer' on 'FileReader': parameter 1 is not of type 'Blob'.
如何修复它。或者,读取图像文件的正确方法是什么? 这是我的“指令”

var userPhotoUpload = function () {

    return {

        link : function (scope, element, attrs) {

            var photoInput = element.find('.uploadField');
            var r = new FileReader();

            element.on('click', function () {

                photoInput[0].click();

            });

            photoInput.on('change', function ( e ) {

                var data = event.target.result;
                r.readAsArrayBuffer(photoInput[0]);

                // console.log( "data", data, e.target.result );

            })

            // r.onloadend = function(e){

   //               var data = e.target.result;
   //               console.log( "let data load", data );
   //           //send you binary data via $http or $resource or do anything else with it
    //      }

        }

    }

}

angular.module("tcpApp")
    .directive('userPhotoUpload', userPhotoUpload );

为了上传http请求上的数据,您可以采用不同的方式 作为一个文件,作为一个字节数组,二进制但简单的方法是作为一个文件使用以下方法是从输入文件 请参见如何使用documenda变量绑定指令

        $http(              
                { method: 'POST',
                  url: 'public/Game/store',
                  // headers:   {    'content-type': 'multipart/form-data'},
                  data: documentData
         });

你可以通过ng flow的gallery上传来实现