joltup / rn-fetch-blob

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

Bad respInfo object / wrong filename on pdf download (android only) #372

Open vitosorriso opened 5 years ago

vitosorriso commented 5 years ago

"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!

fredrik-jarnbrost commented 5 years ago

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