RonRadtke / react-native-blob-util

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

Error: Download interrupted #268

Open FarrukhAnwar1 opened 1 year ago

FarrukhAnwar1 commented 1 year ago

RN Project Version: 0.72.1 ReactNativeBlobUtil Version: ^0.18.3

I've created a function for my React Native application that downloads a webm audio file given a URL (passed URL returns audio/webm) and filename (passed filename always ends in .webm). While it sometimes completely downloads the audio file correctly to the downloads folder, other times it stops halfway and throws Error: Download interrupted. I've checked the URLs that lead to these errors and they're still visible in the browser so I don't understand why this error is happening. Examples of URLs which lead to these errors include:

I don't know if these links are still going to be live when this issue is seen as they have an expiration date. By the way, these links are generated by the react-native-ytdl library (https://www.npmjs.com/package/react-native-ytdl) version ^4.8.3.

Function Definition:

const downloadFile = async (url: string, fileName: string) => {
        try {
            const storagePermissionGranted = await requestStoragePermission();
            if (storagePermissionGranted) {
                const dirs = RNFetchBlob.fs.dirs;
                const fileDir = Platform.OS === "android" ? dirs.DownloadDir : dirs.DocumentDir;
                const filePath = `${fileDir}/${fileName}`;

                const options = {
                    path: filePath
                };

                const task = ReactNativeBlobUtil.config( options ).fetch("GET", url, { "Cache-Control": "no-store", "Accept": "audio/webm" }); 
                task.then(result => {
                    console.log("Main file downloaded to: ", result.path());
                }).catch((error) => {
                    console.error(`Error while downloading main file ${fileName} with primary address ${url} and path ${filePath}:`, error);
                });
            }
        } catch (error) {
            console.error("Error while requesting storage permission:", error);
        }
};
RonRadtke commented 1 year ago

Do you see this error while debugging yourself? Depending on the file size it could be that e.g. a change from wifi to mobile network happens and the download gets interrupted And does it happen on Android or ios?

FarrukhAnwar1 commented 1 year ago

Yeah, I saw it while debugging myself. I'm pretty sure my phone was connected to WIFI the entire time. It happened on Android.

marry1822 commented 1 year ago

I have a similar issue on IOS, I can't download files more than 10Mb, the download process stops halfway but there are no errors, just stops and that's it. Looks like an infinite download. I tried with WIFI and mobile network, no difference.

At the same time, files less that 10Mb are downloaded successfully.

Do you see this error while debugging yourself? Depending on the file size it could be that e.g. a change from wifi to mobile network happens and the download gets interrupted And does it happen on Android or ios?

My code:

const downloadFile = async (fileUrl, fileExt, fileName, fileId) => {
    const {
      exists,
      unlink,
      dirs: {DownloadDir, DocumentDir},
    } = ReactNativeBlobUtil.fs;
    const {config} = ReactNativeBlobUtil;
    const isIOS = Platform.OS == 'ios';
    const aPath = Platform.select({ios: DocumentDir, android: DownloadDir});
    const fPath = `${aPath}/${fileName}${fileExt}`;
    const configOptions = Platform.select({
      ios: {
        fileCache: true, 
        path: fPath,
        notification: true,
        overwrite: true,
      }})
    exists(fPath)
      .then(isExist => {
        if (isExist) {
          return unlink(fPath);
        } else return;
      })
      .then(() => resourceAPI.downloadFile(login, fileId))
      .then(res => {
        if (res.status === 200) {
          if (isIOS) {
            config(configOptions)
              .fetch('GET', fileUrl, {
                Authorization: `Basic ${base64.encode(
                  `${login.username}:${login.password}`,
                )}`,
              })
              .progress({count: 10}, (received, total) => {
                setProgress(received);
              })
              .then(res => {
                setTimeout(() => {
                  ReactNativeBlobUtil.ios.previewDocument(res.path());
                }, 300);
              })
              .catch(error => {
                console.log(error);
                Alert.alert(
                  `Error:\n ${error}`,
                );
              });