Skip to content
This repository was archived by the owner on Mar 16, 2019. It is now read-only.

how to stop download #256

Closed
budingts opened this issue Feb 8, 2017 · 5 comments
Closed

how to stop download #256

budingts opened this issue Feb 8, 2017 · 5 comments

Comments

@budingts
Copy link

budingts commented Feb 8, 2017

when the download is starting,how to stop download

@wkh237
Copy link
Owner

wkh237 commented Feb 8, 2017

@JuanQ123 , please refer to our wiki

@budingts
Copy link
Author

budingts commented Feb 8, 2017

aaa
I use like this,but

“this.task.cannel is not a function”

@wkh237
Copy link
Owner

wkh237 commented Feb 8, 2017

@JuanQ123 , try to separate the statement into lines like this and see if it works

download() {
  this.task = RNFetchBlob.config(...).fetch()
  this.task.progress(...)
  this.task.then(...)
}

stop() {
  this.task.cancel()
}

@bhuizi
Copy link

bhuizi commented Sep 27, 2017

@budingts

This issue is that the .fetch() is in a different scope and that .cancel() needs to be called on .fetch(). From the docs:


task.then((data) => {
  // .. success
})
.catch((err) => {
  console.log(err)
})

// cancel the HTTP request
task.cancel((err, taskId) => {
  // task successfully canceled
})```

task has already returned the .fetch() call with attached promise that will either resolve/reject which allows task to be thenable.
example being:

```class example extends Component {
  super()
  this.handleDownload = this.handleDownload.bind(this);
  this.handleCancel = this.handleCancel.bind(this);
  this.task = RNFetchBlob
  .config({fileCache : true, path : `${dirs.DocumentDir}/example-app/content.zip`})
  .fetch('GET','http://someurl.com');

  handleCancel() {
    this.task.cancel((err, taskId) => {
      console.log(`Cancel: taskId ${taskId}`);
    })
    this.setState({progressNumber: 0});
  }
  
  handleDownload(){
    if(!this.task.onProgress){
      this.task
        .progress({ interval : 100 }, (received, total) => {
          const progressNumber = Math.floor(received/total*100)/100;
          this.setState({progressNumber});
       })
      .then(res => {
        console.log(res);
        })
      .catch((errorMessage) => {
        console.log(errorMessage);
      })
      } else {
        console.log('no task');
      }
    }```

@Johncy1997
Copy link

this.task.cancel() calls .then() method.How to know the response is stopped in cancel method?

zombierabbit pushed a commit to zombierabbit/react-native-fetch-blob that referenced this issue Feb 12, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants