如何使用Kendo文件上传异步方法获得响应?

时间:2015-11-19 13:41:50

标签: javascript angularjs kendo-upload

我正在使用kendo从客户端上传文件一切正常,在加载时正常工作,但是我在上传文件时遇到与url相关的问题我在配置中使用url时文件成功加载我有来自服务器的响应。

如何使用kendo async saveUrl方法从响应中提取数据?

main.html中

<div class="form-group col-md-6">
    <div class="col-md-6">
        <label for="prcFileUploadField">File:</label>
    </div>
    <div class="col-md-6">
        <input name="file" type="file" kendo-upload="fileAttachment" k-upload="addMorePostParameters" k-success="onSuccess" k-error="onError" k-options="fileAttachmentOptions" k-select="onSelect" k-remove="onUploadRemove" />
    </div>
</div>

ctrl.js

angular.module('App').controller('PrcUploadCtrl',function($scope,$timeout,$rootScope,prcUploadConfig){
    'use strict';
    var fileData = [];
    $scope.fileAttachmentOptions = prcUploadConfig.fileAttachmentConfig;

    $scope.prcUploadGridOptions = prcUploadConfig.getPrcUploadDataGrid;
    prcUploadConfig.getPrcUploadDataGrid.dataSource='';

    $scope.onSelect = function (e) {
        fileData = e.files;
        var message = $.map(e.files, function(file) { return file.name; }).join(", ");
        console.log(message);
        console.log(JSON.stringify(fileData));
    };

    //if error 
    $scope.onError = function() {
        console.log("loggin error");
        $timeout(function () {
            var filesToBeRemoved = $scope.fileAttachment.wrapper.find('.k-file');
            $scope.fileAttachment._removeFileEntry(filesToBeRemoved);
        },5000);
    }
});

config.js

fileAttachmentConfig: {
  async: {
      saveUrl: 'app/upload/uploadAttch',
      removeUrl: 'remove',
      removeVerb: 'DELETE',
      autoUpload: false
  },
  template: '<span class=\'file-name-heading\'>Name:</span> <span>#=name#</span><button type=\'button\' class=\'k-upload-action\'></button>'
}

1 个答案:

答案 0 :(得分:2)

您可以随时通过以下成功请求获取响应正文:

$scope.onSuccess = function(e) {
    var response = e.response;
    //...
}