0

I use library react-native-blob-util for downloading file and saving it in the memory of the phone. It works fine on both, android and ios. However I also wish to give the file particular filename and here I have problem as for android it still works good but for ios I have file "ReactNativeBlobUtilTmp_****". How to change it to have normal filename?

export const downloadFile = async (url: string, fileName: string, description: string) => {
  const { config, android, ios, fs } = RNFetchBlob
  const mimeType = 'application/pdf'
  const downloadDir = Platform.OS === 'ios' ? fs.dirs.DocumentDir : fs.dirs.DownloadDir
  const date = new Date()
  const options = {
    fileCache: true,
    addAndroidDownloads: {
      //Related to the Android only
      useDownloadManager: true,
      mediaScannable: true,
      notification: true,
      path: `${downloadDir}/${fileName}_${Math.floor(date.getTime() + date.getSeconds() / 2)}.pdf`,
      description,
      mimeType
    },
    ios: {
      //Related to the IOS only
      path: `${downloadDir}/${fileName}_${Math.floor(date.getTime() + date.getSeconds() / 2)}`,
      description,
      mimeType
    },
    appendExt: 'pdf'
  }

  await config(options)
    .fetch('GET', url)
    .then((res) => {
      // to open file after download
      if (Platform.OS === 'ios') {
        ios.openDocument(res.data)
      } else {
        android.actionViewIntent(res.path(), mimeType)
      }
    })
}

enter image description here

EDIT: I found out that it might be because on ios I need to save file somewhere and this weird name is taken from some tmp place it is stored. Nonetheless it also doesn't work, I modified if ios condition to code below, where path is variable with the path I used above. If I set encoding as base64 it doesn't want to save at all, with utf8 it does save (or at least doesn't throw error) however file is empty:

fs.writeFile(path, res.data, 'utf8')
.then(() => ios.openDocument(path)) 
Jacki
  • 632
  • 3
  • 14
  • 26

0 Answers0