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

How to catch something when downloading is interruppted? #264

Open
liuxiaoy opened this issue Feb 14, 2017 · 15 comments
Open

How to catch something when downloading is interruppted? #264

liuxiaoy opened this issue Feb 14, 2017 · 15 comments

Comments

@liuxiaoy
Copy link

    let task = RNFetchBlob.config({
        path : path
    }).fetch('POST', url, {
        'Content-Type': 'octet-stream'
    });
    task.progress(progressCallback);// listen to download progress event
    task.then((response) => {
        if (response.respInfo.status >= 200 && response.respInfo.status < 300) {
            callback(null, response.path());
        } else
            callback({error: 'fail', message: 'Server Exception ' + response.respInfo.status || ''});
    }).catch(e => {
        console.log(e);
        callback({error: 'fail', message: '当前网络无法访问'});
    });

when the downloading task is interruppted or canceled,it will run to " callback(null, response.path());" always.

@wkh237
Copy link
Owner

wkh237 commented Feb 14, 2017

@liuxiaoy , thanks fro reporting this issue. Does it happen on both Android and IOS ?

@liuxiaoy
Copy link
Author

liuxiaoy commented Feb 14, 2017

@wkh237 Sorry! I have only try it on android.

@wkh237
Copy link
Owner

wkh237 commented Feb 14, 2017

@liuxiaoy , I've setup some tests on this issue but unfortunately still not be able to replicate the problem you addressed.

The behavior was just as expected, catch block executed upon timeout, socket close, and server unreachable. Could you provide a more detailed test case?

@liuxiaoy
Copy link
Author

liuxiaoy commented Feb 14, 2017

@wkh237
step1:RNFetchBlobReq.java
...
case FileStorage:
try {
// In order to write response data to destPath we have to invoke this method.
// It uses customized response body which is able to report download progress
// and write response data to destination path.
resp.body().bytes();
} catch (Exception ignored) {
// ignored.printStackTrace();
}
...
step2:ResponseBody.java in okhttp-3.4.1.sources.jar
...
public final byte[] bytes() throws IOException {
long contentLength = contentLength();
if (contentLength > Integer.MAX_VALUE) {
throw new IOException("Cannot buffer entire body for content length: " + contentLength);
}

BufferedSource source = source();
byte[] bytes;
try {
  bytes = source.readByteArray();
} finally {
  Util.closeQuietly(source);
}
if (contentLength != -1 && contentLength != bytes.length) {
  throw new IOException("Content-Length and stream length disagree");
}
return bytes;

}
...
step3:RealBufferedSource.java in okio-1.9.0-sources.jar
...
@OverRide
public byte[] readByteArray() throws IOException {
buffer.writeAll(source);
return buffer.readByteArray();
}
...

If the downloading task was canceled or interrupered,it will run to " throw new IOException("Content-Length and stream length disagree");" on step2. But the Exception ignored is ignored on step1. At last it seems like the task success in javascript.

@liuxiaoy
Copy link
Author

"react-native-fetch-blob": "^0.10.0",
"react": "^15.4.1",
"react-native": "^0.39.0",

@wkh237
Copy link
Owner

wkh237 commented Feb 14, 2017

Okay, I can replicate it now, this only happens when interrupting a request which downloads data to storage, will look into this issue.

@liuxiaoy
Copy link
Author

liuxiaoy commented Feb 14, 2017

@wkh237 It seems like we have used the same code. But it will catch error when the task is canceled or interruptered.Now I resolve it as follows.
step1:RNFetchBlobReq.java
...
case FileStorage:
try {
// In order to write response data to destPath we have to invoke this method.
// It uses customized response body which is able to report download progress
// and write response data to destination path.
resp.body().bytes();
} catch (Exception ignored) {
// ignored.printStackTrace();
}
this.destPath = this.destPath.replace("?append=true", "");
if (resp.body().contentLength() != new File(this.destPath).length()){
callback.invoke("RNFetchBlob failed to read response data", null);
break;
}
callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_PATH, this.destPath);
break;
step2:
this.refs['progress_dialog'] && this.refs['progress_dialog'].close();
if (err){
console.log(err);
if (err.error == "fail" && this.downloadTask)
Lib.toast(err.message);
delete this.downloadTask;
return;
}
delete this.downloadTask;
Common.installApk(path);
Lib.exitApp();

@wkh237
Copy link
Owner

wkh237 commented Feb 16, 2017

@liuxiaoy , thanks for the information, I've applied a fix on commit 6520e09 you may upgrade your package from github:wkh237/react-native-fetch-blob-package#0.10.3-fix-264 and verify if it works.

$ npm install --save github:wkh237/react-native-fetch-blob-package#0.10.3-fix-264
$ react-native link

@CarGuo
Copy link

CarGuo commented Mar 22, 2017

I am install 0.10.3-fix-264, but i can not found the code change?

@liuxiaoy
Copy link
Author

The changes are in com.RNFetchBlob.RNFetchBlobReq.java
...
case FileStorage:
...

@Compulsed
Copy link

@wkh237 I've installed both 0.10.4 & 0.10.3-fix-264 (RN 0.42.0) and the problem is still happening.

That is if the network connection drops out on Android it returns with status 200 and would behave exactly like it was successful mimicking @liuxiaoy problems.

This is not an issue on iOS and it behaves as you would expect, an error being thrown when the network connection is lost.

Nevertheless, thanks for the awesome project!

@wkh237
Copy link
Owner

wkh237 commented Apr 5, 2017

@Compulsed , thanks for the information, will keep working on this issue 👍

@Compulsed
Copy link

@wkh237 Thanks! Keep up the good work! 👍

@Batistleman
Copy link

We're also seeing this issue. Workaround: comparing the content-length and file on disk manually.

@petewilkins
Copy link

@wkh237 Has there been any update on this?

We are also having the issue on Android where if you download a file and drop network connection midway through, it returns with the same status as if the download was successful. This doesn't happen on iOS, where we get a message:

FAILED - The network connection was lost.

Many thanks 👍

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

6 participants