1

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

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

这是做了什么:

profile.edit.html:

<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>

控制器.js:

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

    $scope.processFiles = function(file, event, flow) {
        flow.defaults.headers = function(file, chunk, isTest) {
            return {
                'X-CSRFToken': $cookies.get("csrftoken")
    };}

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

干杯!

4

1 回答 1

1

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

它应该是:

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

或者:

.html 文件:

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

.js 文件:

 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();
   };
}
于 2015-12-22T11:39:38.563 回答