joltup / rn-fetch-blob

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

Download file from an url (IOS) #691

Closed 15110011 closed 3 years ago

15110011 commented 3 years ago

Hi! I was trying to download a file from an URL like this ( https://xsd.ltd/6HOjk ), the problem that I got is when calling API to download the device (or simulator) can not detect the mime type, and then after downloading it is just a blank file like this: image It was working well on the android devices, maybe on android it automatically detects the mime type. Here my package version: "react-native": "0.62.2", "rn-fetch-blob": "^0.12.0" Did you guys get this situation yet? Here is my code:

const savedata = () => { let {url, fileName, type} = fileDetails; const { dirs: {DownloadDir, DocumentDir}, } = RNFetchBlob.fs; const {config} = RNFetchBlob; const isIOS = Platform.OS === "ios"; const aPath = Platform.select({ios: DocumentDir, android: DownloadDir});

const fPath = aPath + "/" + fileName
const configOptions = Platform.select({
  ios: {
    fileCache: true,
    path: fPath,
    mime: type,
  },

  android: {
    fileCache: false,
    addAndroidDownloads: {
      useDownloadManager: true,
      notification: true,
      path: fPath,
      mime: type,
    },
  },
});

if (isIOS) {
  setIsDownloading(true);
  config(configOptions)
    .fetch('GET', url, {
      'Cache-Control': 'no-store',
      'Content-Type': 'multipart/form-data',
    })
    .then(res => {
      setIsDownloading(false);
      RNFetchBlob.ios.previewDocument('file://' + `${res.path()}`);
    })
    .catch(errorMessage => {
      setIsDownloading(false);
    });
} else {
  setIsDownloading(true);
  config(configOptions)
    .fetch('GET', url, {
      'Cache-Control': 'no-store',
      'Content-Type': 'multipart/form-data',
    })
    .then(res => {
      setIsDownloading(false);
      RNFetchBlob.android.actionViewIntent(res.path());
    })
    .catch((errorMessage, statusCode) => {
      setIsDownloading(false);
    });
}