0

When trying to load files previously uploaded from the server the following error is produced.

// Load existing files:
$.ajax({
    url: $('#fileupload').fileupload('option', 'url'),
    dataType: 'json',
    context: $('#fileupload')[0]
}).done(function (result) {
    $(this).fileupload('option', 'done').call(this, $.Event('done'), {result: files});
});

//Exception
jquery.fileupload-angular.js:130 Uncaught TypeError: Cannot read property '$apply' of undefined
    at done (jquery.fileupload-angular.js:130)
    at <anonymous>:2:47

The following snippet is in the FileUploadController which puts the $scope into the file data.

$element.fileupload(angular.extend(
  {scope: $scope},
  fileUpload.defaults
)).on('fileuploadadd', function (e, data) {
    data.scope = $scope;

1 Answer 1

1

So a solution "hacky" was to inject a service into the controller for the directive and call the generateFileObject(files) method.

.directive('ngUploadForm', [function () {
  return {
    restrict: 'E',
    templateUrl: 'templates/uploadform.html',
    scope: {
      allowed: '@',
      url: '@',
      autoUpload: '@',
      sizeLimit: '@',
      ngModel: '=',
      name: '@'
    },
    controller: ['$scope', '$element', 'fileUpload','FileService', function ($scope, $element, fileUpload, FileService)

[other code]

// pre-populate file list
FileService.getSavedFiles()
  .done(function(files){
    generateFileObject(files)
  });

FileService Code (basic jquery ajax)

this.getSavedFiles = function(){
    return $.ajax({
      url: this.getFileUrl(),
      dataType: 'json'
    });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.