如何将上传图像发送到服务器AngularJS

时间:2016-12-27 15:25:07

标签: javascript angularjs post file-upload

向服务器发出请求

{
    "body":{
        "user_id":"2",
        "message":"sdfdsfsd",
        "images":["http://hylaa-oss.oss-cn-shanghai.aliyuncs.com/icon/hylaa_583d2b9b8e280.jpeg","http://hylaa-oss.oss-cn-shanghai.aliyuncs.com/icon/hylaa_583d2b9b8e280.jpeg"],
        "feedback_id":1

    }
}

我的HTML

<div class="gt-clear">
            <div ng-repeat="step in stepsModel">
                <img width="100" height="100" class="thumb" ng-src="{{step}}" />
            </div>
            <input type='file' ng-model-instant onchange="angular.element(this).scope().imageUpload(this)" />
             <div class="gt-left"><a href="#" class="add-btn-big"></a></div>
             <div class="gt-right btndown-reply"><a class="gt-btn-small"
                        ng-click="addReply(content.user_id, content.feedback_id, textareareply)">Reply</a>
             </div>
        </div>

需要将图像发送到服务器。 我的JS:

$scope.stepsModel = [];

    $scope.imageUpload = function(element){
        var reader = new FileReader();
        reader.onload = $scope.imageIsLoaded;
        reader.readAsDataURL(element.files[0]);
    }

    $scope.imageIsLoaded = function(e){
        $scope.$apply(function() {
            $scope.stepsModel.push(e.target.result);
        });
    }
    console.log($scope.stepsModel);





    $scope.addReply = function(id,feedid,text ) {
    var url = feedbackApiSet.reply;
      var data = {"body":{course_id: id, message: text, images:$scope.stepsModel , feedback_id: feedid }};
      console.log(data);
      httpService.postService(url, data).then(function(res) {
                console.log(res);
                $state.reload();

            })         


  }

1x1像素图像正在上升。因为它的名字很短 发现错误。它转换为base64。并且文件名很长。如何禁用转换为base64?

0 个答案:

没有答案