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 784 forks source link

[Error: document is not supported] cannot open .xlsx document on iOS. #668

Open MannySauce opened 4 years ago

MannySauce commented 4 years ago

Im fetching an excel file generated by server, then gives the user the option to open it after its downloaded.. it works perfectly fine on android, but on iOS it only downloads the file to the File App I can then open it in there, but we would like for it to open from app. I stripped some of the android code out and tried it again but it still gives the problem. I have also added the 2 necessary keys in info.plist, and did the pod thing.. any help is appreciated thanks

<key> UIFileSharingEnabled</key>
<true/>
<key>LSSupportsOpeningDocumentsInPlace</key>
<true/>
handleDownloadPress = async () => {
    this.setState({isDownloading: true, optionsVisible: false});
    const {startDate, endDate} = this.state;
    let dirs = RNFetchBlob.fs.dirs;
const android = RNFetchBlob.android;
const filename = `Reporte${startDate}-${endDate}.xlsx`;
const platPath = Platform.OS === "android" ? dirs.DownloadDir : dirs.DocumentDir;
const path = platPath + '/' + filename;

RNFetchBlob.config({
  addAndroidDownloads: {
    useDownloadManager: true,
    path,
    notification: true,
  },
  path,
  fileCache: true
})
  .fetch(
    'GET',
    `${BASE_URL}rest/pedidos/generateExcel?initDate=${startDate}&endDate=${endDate}T23:59:59`,
    {
      token: this.props.session.access_token,
      idrest: this.props.session.info_rest.id,
    },
  )
  .then((res) => {
    //shows a modal with a button 
    this.alertConfig.onBtnPress = ()=>{
      if(Platform.OS === "android"){
        android.actionViewIntent(res.path(), 'application/vnd.ms-excel');
      }
      else {
        RNFetchBlob.ios.openDocument(res.path());
      }
    }
    this.setState({isDownloading: false, isAlertVisible: true});//show modal
  })
  .catch((errorMessage, statusCode) => {
   //printStatements, toggleing states
  });

};

dhananjaya90 commented 3 years ago

I have same issue,

On a physical device (iPhone 11 - iOS 13.5) When I try to execute openDocument or previewDocument both fails and crashes the app without giving any error in the debug console. Then I tried with the catch block and the exception says the file not supported.

On the simulator, It opens up the document but doesn't show its contents image

sunn-e commented 3 years ago

@MannySAD @dhananjaya90 Did you find any way to work around this?

MannySauce commented 3 years ago

@MannySAD @dhananjaya90 Did you find any way to work around this?

What I wanted to do initially was to download the file to the file app and if the user wanted to view it he would have to press a button I had inside an alert/modal, I investigated a bit and found out people were having the same problem (opening recent downloaded files on button press inside modal) so I switched to RNFetchBlob.ios.previewDocument(res.path()); (only for ios) now once the user presses the download button the native ios share menu pops up instead of the modal.. the user can then choose if he only wants to store it or store and open it through the excel app. But im thinking this is situational to what you want, I left it like this because I didnt find a solution to open it directly.

linhkiubo98 commented 2 years ago

@dhananjaya90 can u help me

linhkiubo98 commented 2 years ago

this is my code ,on IOS crash app let dirs = RNFetchBlob.fs.dirs; const url = item.url; RNFetchBlob.config({ path: dirs.DocumentDir + url.slice(url.lastIndexOf('/'), url.length), addAndroidDownloads: { useDownloadManager: true, title: url.slice(url.lastIndexOf('/') + 1, url.length), mediaScannable: true, notification: true, path: dirs.DocumentDir + url.slice(url.lastIndexOf('/'), url.length), }, fileCache: true, }).fetch('GET', url).then((res) => { console.log(res.data, res.path());

    try {
      RNFetchBlob.ios.openDocument(res.path())  
    } catch (error) {
      console.log('xxxxxxxxxxx',error);

    }
  });
jyotiprakash111 commented 1 year ago

Guys Can somebody please help me with the same, for me everything is working on iPhone SE(3rd Generation), but not working on iPhone 14.

RN Version - 71.8 Rn-Fetch-Blob - 0.12.0