react-native-blob-util icon indicating copy to clipboard operation
react-native-blob-util copied to clipboard

IOS download document problem

Open MuratAkbaba006 opened this issue 2 years ago • 8 comments

Hi, I try to download file in ios but I can't see file. in Andriod everthing OK. my versions: "react-native": "^0.68.2", "react-native-blob-util": "^0.16.0",

this my download function async function handleDownload(val) { try { const fileExtension = val.workorderDocumentUrl.split('.')[1]; const fileName = /${val.workorderDocumentName}.${fileExtension}`;

    const {workorderDocumentGuid, workorderDocumentName} = val;

    const configOptions = Platform.select({
      ios: {
        fileCache: true,
        path: ReactNativeBlobUtil.fs.dirs.DocumentDir + fileName,
        notification: true,
        IOSDownloadTask: true,
      },
      android: {
        fileCache: false,
        addAndroidDownloads: {
          useDownloadManager: true,
          notification: true,
          path: ReactNativeBlobUtil.fs.dirs.DownloadDir + fileName,
          description: 'Downloading...',
        },
      },
    });
    if (Platform.OS === 'ios') {
      ReactNativeBlobUtil.config(configOptions)
        .fetch(
          'GET',
          `${store.baseUrl}/docs?guid=${workorderDocumentGuid}`,
          {
            'accept-version': 'v3',
            'inavitas-tenant': store.tenant,
          },
        )
        .progress((received, total) => {
          console.log('PROGRESS', received, total);
        })
        .then(res => {
          setTimeout(() => {
            var filePath = res.path();
            ReactNativeBlobUtil.ios.presentPreview('file://' + filePath);
            console.log('RESIOS', res);
            //ReactNativeBlobUtil.ios.openDocument(res.data);
          }, 300);
          // the temp file path
          console.log('The file saved to ', res.path());
          setSuccessDownload(true);
          console.log('DIRS', ReactNativeBlobUtil.fs.dirs);
        })
        .catch(err => console.log('download Err', err));
    } else {
      ReactNativeBlobUtil.config(configOptions)
        .fetch(
          'GET',
          `${store.baseUrl}/docs?guid=${workorderDocumentGuid}`,
          {
            'accept-version': 'v3',
            'inavitas-tenant': store.tenant,
          },
        )
        .then(res => {
          ReactNativeBlobUtil.android.actionViewIntent(res.path());
          // the temp file path
          console.log('The file saved to ', res.path());
          setSuccessDownload(true);
          console.log('DIRS', ReactNativeBlobUtil.fs.dirs);
        })
        .catch(err => console.log('download Err', err));
    }
  } catch (err) {
    console.log(err);
  }
}`

Here is my debug console, I can see directory && process Screen Shot 2022-06-03 at 11 21 58

MuratAkbaba006 avatar Jun 03 '22 08:06 MuratAkbaba006

For iOS, you can use react-native-share once you download the file to save it to files.

  .then((res) => {
        // the temp file path
        console.log('The file saved to ', res.path());
        // in iOS, we want to save our files by opening up the saveToFiles bottom sheet action.
        // whereas in android, the download manager is handling the download for us.
        if (Platform.OS === 'ios') {
          const filePath = res.path();
          let options = {
            type: 'application/pdf',
            url: filePath,
            saveToFiles: true,
          };
          Share.open(options)
            .then((resp) => console.log(resp))
            .catch((err) => console.log(err));
        }
      })

Saad-Bashar avatar Jun 23 '22 02:06 Saad-Bashar

On iOS you won't be able to see the files in the Files app unless you share them

RonRadtke avatar Jun 25 '22 05:06 RonRadtke

For iOS, you can use react-native-share once you download the file to save it to files.

  .then((res) => {
        // the temp file path
        console.log('The file saved to ', res.path());
        // in iOS, we want to save our files by opening up the saveToFiles bottom sheet action.
        // whereas in android, the download manager is handling the download for us.
        if (Platform.OS === 'ios') {
          const filePath = res.path();
          let options = {
            type: 'application/pdf',
            url: filePath,
            saveToFiles: true,
          };
          Share.open(options)
            .then((resp) => console.log(resp))
            .catch((err) => console.log(err));
        }
      })

or use this

ReactNativeBlobUtil.ios.previewDocument(file.path());

ddikodroid avatar Jul 28 '22 12:07 ddikodroid

@Saad-Bashar i had the same problem, appending .pdf to the path fixes the problem for me

path: ${dirs.DocumentDir}/${fileName}.pdf,

LOG  The file saved to  /Users/xx/Library/Developer/CoreSimulator/Devices/FEBCC312-3D3A-4C60-A008-49A488000139/data/Containers/Data/Application/6DE346D7-CC55-4E21-B9F5-1D9766E977EB/Documents/Resources-1701931717558
LOG  The file saved to  /Users/xx/Library/Developer/CoreSimulator/Devices/FEBCC312-3D3A-4C60-A008-49A488000139/data/Containers/Data/Application/6DE346D7-CC55-4E21-B9F5-1D9766E977EB/Documents/Resources-1701931752795.pdf

anniewey avatar Dec 07 '23 06:12 anniewey

Is there an update on this? I tried downloading the file xlsx in iOS, but it always returns the path simulator.

truongnat avatar Jan 03 '24 08:01 truongnat

Is there an update on this? I tried downloading the file xlsx in iOS, but it always returns the path simulator.

I don't know if this is the right solution, but I managed to solve the problem using react-native-share

Share.open({ url: urlPath, saveToFiles: true, }) urlPath is url returned by ReactNativeBlobUtil

Pavel-Tasits avatar Jan 03 '24 13:01 Pavel-Tasits

Is there an update on this? I tried downloading the file xlsx in iOS, but it always returns the path simulator.

I don't know if this is the right solution, but I managed to solve the problem using react-native-share

Share.open({ url: urlPath, saveToFiles: true, }) urlPath is url returned by ReactNativeBlobUtil

Thank you for your response,

I fixed the issue in iOS. I'm not sure why it didn't download, but I am using the react native file viewer to open it and it's working. The user will need to download a manual.

truongnat avatar Jan 04 '24 02:01 truongnat

For iOS, you can use react-native-share once you download the file to save it to files.

  .then((res) => {
        // the temp file path
        console.log('The file saved to ', res.path());
        // in iOS, we want to save our files by opening up the saveToFiles bottom sheet action.
        // whereas in android, the download manager is handling the download for us.
        if (Platform.OS === 'ios') {
          const filePath = res.path();
          let options = {
            type: 'application/pdf',
            url: filePath,
            saveToFiles: true,
          };
          Share.open(options)
            .then((resp) => console.log(resp))
            .catch((err) => console.log(err));
        }
      })

The only way I've made it worked was using this approach.

For iOS, you can use react-native-share once you download the file to save it to files.

  .then((res) => {
        // the temp file path
        console.log('The file saved to ', res.path());
        // in iOS, we want to save our files by opening up the saveToFiles bottom sheet action.
        // whereas in android, the download manager is handling the download for us.
        if (Platform.OS === 'ios') {
          const filePath = res.path();
          let options = {
            type: 'application/pdf',
            url: filePath,
            saveToFiles: true,
          };
          Share.open(options)
            .then((resp) => console.log(resp))
            .catch((err) => console.log(err));
        }
      })

or use this

ReactNativeBlobUtil.ios.previewDocument(file.path());

I've tried this too but it didn't work for me. Thank you @Saad-Bashar !

jmada avatar Mar 27 '24 15:03 jmada