Rn-fetch-blob: Bad respInfo object / wrong filename on pdf download (android only)

Created on 8 Jun 2019  Â·  1Comment  Â·  Source: joltup/rn-fetch-blob

"react": "16.6.3",
"react-native": "0.57.8",
"rn-fetch-blob": "^0.10.15",

This is my PDFUtils.js, to handle PDF download/view in my app from a remote source. This code was working perfectly until some days ago.

Right now, this code is working perfectly as expected on iOS.
On Android, this still downloads the pdf, but with 2 unexpected behaviours:

  1. Filename is wrong (now it taked the notification title ad the filename)
  2. This throws me an error, because response.respInfo.status is undefined!

So I console-logged the response object, and I noticed that the android response object is changed!

ANDROID:
response_android

IOS:
response_ios

I was looking for response.respInfo.status to see the statusCode of the network response, but this information is not available anymore on android only! Why??

Here is the code:

import { PermissionsAndroid, Platform } from 'react-native'
import RNFetchBlob from 'rn-fetch-blob'
import SHA1Generator from '../SHA1Generator'
import { URLKey, URLContest, URLDomain, baseURL } from 'react-native-dotenv'
import constants from '../../constants'

export const PDFDownloader = async queryParams => {
  const dirs = RNFetchBlob.fs.dirs

  // preparing url
  const URLPath = URLContest + constants.endpoints.printPdfWorkingCareer.split(URLDomain)[0]
  const SHA1Token = SHA1Generator(URLKey, URLPath)
  const downloadURL = baseURL + constants.endpoints.printPdfWorkingCareer + SHA1Token + queryParams

  if (Platform.OS === 'android') {
    await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE)
    await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE)
  }

  let destDir = ''
  if (Platform.OS === 'ios') destDir = dirs.DocumentDir
  else destDir = dirs.DownloadDir
  destDir = destDir + '/LavoroXTe-PercorsoLavoratore.pdf'

  const options = {
    path: destDir,
    fileCache: false,
    addAndroidDownloads: {
      useDownloadManager: true,
      notification: true,
      path: destDir,
      title: 'LavoroXTe',
      description: 'Downloading PDF...'
    }
  }

  const config = {
    'Content-Type': 'application/pdf',
    'Cache-Control': 'no-store'
  }

  const response = await RNFetchBlob.config(options).fetch('GET', downloadURL, config)
  console.log('pdfResponse: ', response)
  if (response.respInfo.status !== 200) throw new Error('Download PDF error')

  return destDir
}

Thanks in advance!

Most helpful comment

I had the same problem, I solved it by not using the download manager.

>All comments

I had the same problem, I solved it by not using the download manager.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

TheWirv picture TheWirv  Â·  13Comments

singhlovey picture singhlovey  Â·  12Comments

meiqi1992 picture meiqi1992  Â·  19Comments

fanzhiri picture fanzhiri  Â·  46Comments

wamry picture wamry  Â·  16Comments