ng-flow在标头上设置CSRF令牌

时间:2015-12-22 11:33:31

标签: angularjs ng-flow

在使用带有flow.js的文件上传时,我找不到设置X-XSRF-TOKEN标头的方法。

由于flow.js似乎不使用$ http资源发布文件,我需要明确设置标题。

这是做了什么:

profile.edit.html:

$(".column").wrapAll("<div class='row'><div class='col-md-6'></div></div>");
$(".row").prepend("<div class='col-md-2'>Name Lastname</div>");

controller.js:

<div class="row" flow-init="{target: 'rest/account/uploadImage'}" 
                        flow-file-added="processFiles($file, $event, $flow)"
                        flow-files-submitted="$flow.upload()" >
...
<img type="file" flow-btn ng-if="$flow.files.length" flow-img="$flow.files[$flow.files.length-1]" class="img-circle m-t-xs img-responsive" />

</div>

但不幸的是,没有任何设定。有什么想法吗?

干杯!

1 个答案:

答案 0 :(得分:1)

Flow.js不使用$ http服务,因此您必须手动为每个请求设置csrf令牌。

应该是:

<div flow-init="{headers: {'XCSRF-TOKEN': csrf}}">
</div>

html文件:

<div flow-init="setToken();">
    ...
</div>

js file:

 function profileCtrl($rootScope, $scope, $http, $location, $window, $cookies, toaster){

    $scope.setToken = function() {
       $scope.options = {
         headers: {
            'X-CSRFToken': $cookies.get("csrftoken");
         }
       };
    }

}

或:

Html文件:

  <div  flow-init flow-files-submitted="uploadFiles($files, $event, $flow);"
                flow-file-added="!!{png:1,gif:1,jpg:1,jpeg:1}[$file.getExtension()]"

                flow-name="flow" flow-upload-started="uploadStart($flow);"
                flow-file-success="fileUploadSucces($file, $message)"
                flow-complete="fileUploadsComplete();">

....
  </div>

JS档案:

 function profileCtrl($rootScope, $scope, $http, $location, $window, $cookies, toaster){

    $scope.uploadStart = function($flow){   
        $flow.opts.target = "rest/account/uploadImage";
        $flow.opts.headers =  {'X-CSRFToken' : $cookies.get("csrftoken")};
    }

    $scope.fileUploadSucces = function($file, $message){
        //Upload Success

    };

    $scope.fileUploadsComplete = function(){
       //Complated Upload

    };

    $scope.uploadFiles = function($file, $message, $flow){
        //Uploading
        $rootScope.$flow = $flow;
        $rootScope.$flow.upload();
   };
}