wkh237 / react-native-fetch-blob

A project committed to making file access and data transfer easier, efficient for React Native developers.
MIT License
2.61k stars 1.59k forks source link

how to pause or resume downloading ? #193

Closed neegqi closed 7 years ago

neegqi commented 7 years ago

To download a large file if the network was disconnect,now i how to resume to download?

wkh237 commented 7 years ago

@neegqi , please refer to #141 .

neegqi commented 7 years ago

Thank u so much

neegqi commented 7 years ago

@wkh237 i used the param "append=true" But it does not seem to work,I used the method progress() to listening progress,

wkh237 commented 7 years ago

@neegqi , I think the information in #141 is a little out of dated. Instead of using append=true, you should add overwrite : true to Fetch.config. Please refer to the document. Thanks !

neegqi commented 7 years ago

@wkh237

Here is my code

 RNFetchBlob.config({
            path: dirs + '/data.mp4?append=true',
            overwrite: true
        }).fetch('GET', 'http://apicloudspace.b0.upaiyun.com/apicloud-video/c1.1.mp4')
            .progress((received, total) => {
                console.log('progress', (received / total).toFixed(2)+"&&&&"+(total/(1024*1024)).toFixed(2))
            })
            .then((res) => {
                // the temp file path with file extension `png`
                console.log(res)
                console.log('The file saved to ', res.path())
                // Beware that when using a file path as Image source on Android,
                // you must prepend "file://"" before the file path

            })

Is there an something wrong in the code? was i ctl+r to reload , console Output the received Always begining from 0

wkh237 commented 7 years ago

@neegqi , if you set overwrite to true, it exactly overwrites old data, I assume you should set it to false and remove ?append=true params.

wkh237 commented 7 years ago

Besides, you should handle HTTP Range Request yourself if you're going to implement somehting like resumable download.

neegqi commented 7 years ago

Resolved , its worked . thanks

fs.exists(dest) .then((ext) => { if (ext) { return fs.stat(dest) .then((stat) => stat) } else return Promise.resolve({ size: 0 }) }) .then((stat) => { console.log(stat) return RNFetchBlob .config({ path: dest, overwrite: false }) .fetch('GET', 'http://apicloudspace.b0.upaiyun.com/apicloud-video/c1.1.mp4', { Range:bytes=${stat.size}- }) .progress((received, total) => { const ss = 1024 * 1024; console.log('progress', (received / total).toFixed(2) + "&&&&" + (total / ss).toFixed(2)) }) })