0

I'm using AngularJS and setting ngSrc on an iframe, which I use to download a zipped archive to get around not being able to download files using AJAX. I'm using a directive for detecting the iframe load that I found here, which works great when you load a URL like "about:blank", but it doesn't have a load when I hit it with a RESTful call to download the file, even though it downloads the generated file.

// Markup of iframe using ngSrc and custom directive
<iframe ng-src="{{url}}" style="display:none;" iframe-onload="myCallback" />

// Controller setting iFrame, what I'd like to trigger the load
$scope.downloadArchive = function( id ) { // no load event? but downloads zip archive

    var url = '/data/archive/instance/' + id;
    $scope.url = $sce.trustAsResourceUrl( url );
}

// Controller setting iFrame, what does trigger the load
$scope.downloadArchive = function( id ) { // load event triggered

    $scope.url = $sce.trustAsResourceUrl('about:blank');
}

Is is possible to detect an event when downloading a zipped archive through the iframe?

Community
  • 1
  • 1
mtpultz
  • 17,267
  • 22
  • 122
  • 201

2 Answers2

0

Is it that you can't download the file via AJAX due to browser restrictions? Like cross domain issues? You can find out by examining the error in Chrome Console.

If that is the case, you won't be able to view the contents of the file in an iframe either due to modern browser restrictions. Think of what can happen if developers are allowed to access the contents of any iframe. I could make a website with your bank's website in an iframe. Once you log in, I'd be able to listen to a form submit event from the iframe and then get your login credentials.

Hence, what could be happening is that your iframe is loading the file, trying to display the file within it, but intentionally hindering you from viewing its contents for security.

I'd recommend downloading the file via a proxy server you own or through a cloud service, and then serving the file from your own server. This circumvents cross domain issues since you can now ping your own server.

John Hoffman
  • 17,857
  • 20
  • 58
  • 81
  • The zip file actually downloads no problem, but I have no way of detecting if it did, the iframe load event doesn't fire. Need to know when it has downloaded so I can reset the iframes url to "about:blank" or other otherwise I can't download a second copy of the file if required, need to reset the URL to something else first. – mtpultz Nov 06 '14 at 06:30
0

I ended up not trying to reset the URL each time and just appended the current time as a second parameter, which gets ignored by the server. Now it appears like iframe is reloading a new URL.

var url = '/data/archive/instance/' + id + '/' + Date.now();
mtpultz
  • 17,267
  • 22
  • 122
  • 201