Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cancel Method Not working #687

Open
ebeliejinfren opened this issue Oct 22, 2020 · 2 comments
Open

Cancel Method Not working #687

ebeliejinfren opened this issue Oct 22, 2020 · 2 comments

Comments

@ebeliejinfren
Copy link

i use rn-fetch-blob to download file in React Native every thing work correct but when use cancel method according to doc, cancelling not work and got error "fileFetchPromise Is Not Defained"

const download = () => {
  let fileOptions = { fileCache: true, path: path, };
  const fileFetchPromise = config(fileOptions).fetch('GET', fileUrl);
  fileFetchPromise.progress({ interval : 1000 }, (received, total) => {
    let persend = 100 * received / total;
    setSaved(persend.toFixed(0));
    setReceived(bytesToSize(received));
    setTotalSize(bytesToSize(total));
  });

  fileFetchPromise.then((data) => {
    setSaved('100');
  }).catch((err) => {
    setSaved('0')
  })
}

const cancelDownload = () => {
  fileFetchPromise.cancel((err) => {})
}
@nadav2051
Copy link

fixed it in my fork, you can look:
nadav2051@6af66cb
nadav2051@f979791

@majirosstefan
Copy link

majirosstefan commented Dec 22, 2021

@nadav2051 Not working, I used this patch-package diff:

diff --git a/node_modules/rn-fetch-blob/polyfill/Fetch.js b/node_modules/rn-fetch-blob/polyfill/Fetch.js
index 3ecb591..979f714 100644
--- a/node_modules/rn-fetch-blob/polyfill/Fetch.js
+++ b/node_modules/rn-fetch-blob/polyfill/Fetch.js
@@ -40,6 +40,7 @@ class RNFetchBlobFetchPolyfill {
           promise = Blob.build(body).then((b) => {
             blobCache = b
             options.headers['Content-Type'] = 'multipart/form-data;boundary=' + b.multipartBoundary
+            options.headers['content-type'] = 'multipart/form-data;boundary=' + b.multipartBoundary
             return Promise.resolve(RNFetchBlob.wrap(b._ref))
           })
         }
@@ -58,10 +59,14 @@ class RNFetchBlobFetchPolyfill {
       // task.then is not, so we have to extend task.then with progress and
       // cancel function
       let progressHandler, uploadHandler, cancelHandler
+
+      let scopedTask = null
+
       let statefulPromise = promise
           .then((body) => {
             let task = RNFetchBlob.config(config)
-              .fetch(options.method, url, options.headers, body)
+                .fetch(options.method, url, options.headers, body)
+            scopedTask = task
             if(progressHandler)
               task.progress(progressHandler)
             if(uploadHandler)
@@ -86,12 +91,11 @@ class RNFetchBlobFetchPolyfill {
       }
       statefulPromise.cancel = () => {
         cancelHandler = true
-        if(task.cancel)
-          task.cancel()
+        if(scopedTask && scopedTask.cancel)
+          scopedTask.cancel()
       }
 
       return statefulPromise
-
     }
   }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants