1

I am using the react-native-fetch-blob library. However, when a 404 message is sent it creates an empty file and calls it blag6.zip. This is my code:

try {
    response = await RNFetchBlob
    .config({
        path: RNFetchBlob.fs.dirs.DocumentDir + "/temp/blag6.zip"
    })
    .fetch(
        'POST',
        'http://example.com/data/getTestSet.php',
        {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        JSON.stringify({
            'passcode': "pass123",
            'group_id': "1",
            'file': "ShgRG/suvA/blag.zip"
        })
    )


    temp_file = RNFS.DocumentDirectoryPath + "/temp/blag6.zip"
    output_path = RNFS.DocumentDirectoryPath + "/output

    await ZipArchive.unzip(temp_file, output_path)
    console.log("This line is never reached.")
}
catch (err){
    console.log(err)
}

I get the following error:

Error: Failed to extract file File too short to be a zip file: 0

Is there a way to prevent RNFetchBlob from saving empty files and throw an error instead? How can I tell what the error is?

Update:

For now I am using a temporary fix with the following:

status = response.info().status

//check if file exists here, throw error otherwise

file_name = RNFetchBlob.fs.dirs.DocumentDir + "/temp/blag6.zip"

file_stat = await RNFS.stat(file_name);
file_size = file_stat.size;

if (status != 200 || file_size == 0){
    await RNFS.unlink(file_name);
    throw "My error message";
}

Is there a reason that RNFetchBlob still creates a file if none was available? Is there any way to prevent it from creating an empty file if there is no file to save?

David Schumann
  • 13,380
  • 9
  • 75
  • 96
kojow7
  • 10,308
  • 17
  • 80
  • 135
  • Could you try adding the following to your fetch? You should be able to determine if file is empty by the length of base64 it produces. ```.then((res) => { let base64Str = res.base64(); });``` – Tukan Jan 10 '18 at 23:37
  • Hmmm, that's interesting, base64Str seems to contain a Promise object. Why would it contain a promise when `then` should have dealt with it? When displaying the results of base64Str to the console I am getting `Promise {_40: 0, _65: 0, _55: null, _72: null}` – kojow7 Jan 10 '18 at 23:53
  • Yes that is interesting, the snippet is right from the documentation. Could you try to see what is in that Promise (attach another then to it, to see what it holds) – Tukan Jan 11 '18 at 00:33
  • Hmm, even stranger, if I add another .then to the chain and do a `console.log(res)` I get `undefined`. – kojow7 Jan 11 '18 at 02:46
  • Not to the chain, inside the first then you should write ```res.then(console.log)```. – Tukan Jan 11 '18 at 13:45
  • @H.TugkanKibar I now get `TypeError: res.then is not a function` – kojow7 Jan 11 '18 at 15:01
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/162993/discussion-between-h-tugkan-kibar-and-kojow7). – Tukan Jan 11 '18 at 15:02

0 Answers0