1

I have following procedure in my AngularJS app to open a text file in a new tab to save:

$scope.createString()
                .then(function () {
                    var windowReference = window.open();
                    var blob = new Blob([$scope.createdString], { type: 'text/plain' });
                    var url = (window.URL || window.webkitURL).createObjectURL(blob);
                    windowReference.location = url;
                 });

But this does not work. It only opens the same link in a new tab but not the created Blob-Url.

What do I do wrong? How do I accomplish this?

UPDATE:

I accomplished it in IE like this:

$scope.createString()
                .then(function () {
                    var blob = new Blob([$scope.createdString], { type: 'text/plain' });
                    window.navigator.msSaveOrOpenBlob(blob, 'Test.txt');
                 });

But is there a standardized way which works with all browsers?

Palmi
  • 2,381
  • 5
  • 28
  • 65

1 Answers1

0

Another approach I've used to make this work is to use a traditional <a> tag to open the blob, using the target='_blank' attribute.

Once you have the blob object URL, just plug it into the href attribute, and you'll have something that looks like this:

<a target="_blank" href="blob:http://example.com/523d3551-80fd-4fb0-a0fc-5194d928cc6d">Print</a>
Jeffery Bennett
  • 490
  • 1
  • 5
  • 9