Arco Voltaico
Arco Voltaico

Reputation: 814

Make a file writeStream Observable on Angular

I'm struggling trying to guess how can I create a method, that can be subscribed, returning the location of a file, just when it has been downloaded. The method now is wrong : Type 'void' is not assignable to type 'Observable<any>'.

downloadFile(url, location):  Observable<any>{
  const file = fs.createWriteStream(location);
  https.get(url, function (response) {
     response.pipe(file);
     file.on('finish', function (d) {
           file.close();
        return Observable.of(location);
  });
});

Any idea ? Thanks :-)

Upvotes: 1

Views: 1164

Answers (1)

Arco Voltaico
Arco Voltaico

Reputation: 814

I came up with this working solution :

import { NextObserver } from 'rxjs/Observer';

downloadFile(url):  Observable<any>{

return Observable.create((observer: NextObserver <any>) => {
   const file = fs.createWriteStream(location);
  https.get(url, function (response) {
    response.pipe(file);
   });

 file.on('finish', () => {
      file.close();
        observer.next(location);
        observer.complete();
    });
});
}

Upvotes: 1

Related Questions