1

I want to add the following StreamSaver.js code to an AngularJS. The same syntax is not accepted so I have changes the code to suite angular.

I got the following WritableStream is not defined error.

angular-1.5.7.min.js:117 ReferenceError: WritableStream is not defined
    at Object.createWriteStream (StreamSaver.js:92)
    at m.$scope.downloadFile (profile_ctrl.js:305)

The StreamSaver Example code is working fine.

fetch(url).then(res => {
    let reader = res.body.getReader()
    let pump = () => {
        return reader.read().then(({ value, done }) => {
            if (done) {
                myFile.close()
                return
            }
            myFile.write(value)
            return pump()
        })
    }
    pump()
})

Whereas not the modified code given below.

var myFile = streamSaver.createWriteStream("filename.txt");
fetch(url).then(function(res) {
    $scope.pump = function(reader) {
        return reader.read().then(function(value, done) {
            if (done) {
                myFile.close();
                return;
            }
            myFile.write(value);
            return pump();
        });
    };
    var reader = res.body.getReader();
    $scope.pump(reader);
});
7
  • Dose your targeted browser support ES6 syntax? StreamSaver only works in chrome and opera so my examples was written using the latest syntax. You might want to convert it down to ES5 Commented Aug 16, 2016 at 1:18
  • I am getting this syntax error during code compilation. I have an angularjs website want to download files as streams. Chrome v52 is the browser I am using for testing. Commented Aug 16, 2016 at 7:26
  • what syntax error? do you want to use torrent? cuz you are missing interval variable Commented Aug 16, 2016 at 8:58
  • Hi @Endless, I have removed the torrent part and edited the question to reflect current error. Commented Aug 17, 2016 at 10:26
  • WritableStream should be defined in chrome v52, may i ask what version you are using? Commented Aug 17, 2016 at 11:05

2 Answers 2

2

The error is because of ES6 syntax. The following code works for ES5 in chrome v52 browser.

var myFile = streamSaver.createWriteStream("filename.txt");

fetch(url).then(function(res) {
  var reader = res.body.getReader();
  $scope.pump(reader);
});

$scope.pump = function(reader) {
  return reader.read().then(function(result) {
    if (result.done) {
      myFile.close();
      return;
    }
    myFile.write(result.value);
    return $scope.pump(reader);
  });
};
Sign up to request clarification or add additional context in comments.

Comments

1

You would better to use existing packages in angularjs. For example angular-file-saver trying to wrap FileSaver.JS.

1 Comment

filesaver.js has limits which is not good for large media files. Moreover in chrome for android, the maximum file size is amount of RAM available. So thats a major drawback of filesaver.js

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.