-
Notifications
You must be signed in to change notification settings - Fork 1.6k
the downloaded partial file is override when resume breakpoint download #141
Comments
@haibinyu , thank you for the information, I've never considered about this scenario, but it'd be great if we can fix this, I'll try fix it in next release ( probably 0.9.6). There will be a |
@kejinliang , @haibinyu , after doing some tests on both Android and IOS, I think they both acting the same at this moment. The PR #139 changes the behavior so that the response data always appended to the file if it already exists, but I think this change may effect those who use it by intended for replace the existing file. In my opinion, it's better to introduce an option for better backward compatibility. On the other hand, there's also a workaround like this .. const {fs, config} = RNFetchBlob
function getTemporaryPathForDownload(dest) {
return dest + '.download'
}
function resumableDownload(dest) {
let tmpPath = getTemporaryPathForDownload(dest)
fs.exists(tmpPath)
.then((ext) => {
// if the tmp file exists, which means the previous
// download session was interrupted.
// append data in content ${dest}.download to destination
if(ext) {
// specify encoding `uri` is the most performant way
// to perform file-to-file data transfer,
// the whole process is done in native
return fs.appendFile(tmpPath, dest, 'uri')
.then(() =>fs.stat(dest))
}
else
return Promise.resolve({ size : 0 })
})
// after the previous downloaded data moved to desitation (if there is)
// we can start a new session which overwrites the ${dest}.download
// the new seesion will use range request which ask for the remain
// part from server
.then((stat) => {
return RNFetchBlob
.config({ path : tmpPath })
.fetch('GET', 'http://example.com/large-file.zip', {
Range : `bytes=${stat.size}-`
})
})
// if the task completed, write the content to the true destination
.then((res) => fs.appendFile(res.path(), dest, 'uri') )
// remove tmp file ( file at ${dest}.download )
.then(() => {
return fs.unlink(tmpPath)
})
.then(() => {
/* done !! */
})
} |
I agree that it is better to add a config option to enable append file behavior. |
Thanks for the workround, 👍 |
@haibinyu , I've published a In this version, you are able to append response data to existing file by append RNFetchBlob.config({
path : pathOfFile + '?append=true'
})
.fetch('GET', 'http://example.com') Please try if it works, thank you 😄 |
@wkh237 👍 , I will try and let you know the result. |
it works now. |
react native: 0.33.0
react-native-fetch-blob:0.9.5.
In my project, I need to implement file breakpoint download. When the file is download partially (for example 50%), I exit the app, and then open the app to continue the file download, the content already downloaded in the file is gone.
The text was updated successfully, but these errors were encountered: