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 not working on ios for files without extension pdf #698

Open lamaji opened 3 years ago

lamaji commented 3 years ago

RN version

0.61.2

Library version

0.12.0

My Code

var url = "https://exmple.com/dompdf?date=" + this.state.date + "&token=" + token;
const { config, fs } = RNFetchBlob
const dirToSave = Platform.select({
    ios: fs.dirs.DocumentDir,
    android: fs.dirs.DownloadDir
});
let options = {
    fileCache: true,
    addAndroidDownloads: {
        useDownloadManager: true,
        notification: true,
        path: dirToSave + "/filename.pdf",
        description: 'PDF',
        mime: 'application/pdf',
    }
}
try {
    config(options).fetch('GET', url).then((res) => {
        console.log('download OK');
    }).catch((error) => {
        console.log(error.message);
    });
} catch (error) {
    console.log(error.message);
}

Issue

download not working on iOS for files without extension with error message "protocol error" but works on android if I put a file url like this https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf it works fine.

kishoretankhupp commented 3 years ago

@lamaji Did you tried adding appendExt option?

let options = {
    fileCache: true,
    appendExt: 'pdf',
    addAndroidDownloads: {
        useDownloadManager: true,
        notification: true,
        path: dirToSave + "/filename.pdf",
        description: 'PDF',
        mime: 'application/pdf',
    }
}
Ajmal0197 commented 3 years ago

Latest working soln

const actualDownload = () => {
    const { dirs } = RNFetchBlob.fs;
    const dirToSave = Platform.OS == 'ios' ? dirs.DocumentDir : dirs.DownloadDir
    const configfb = {
        fileCache: true,
        useDownloadManager: true,
        notification: true,
        mediaScannable: true,
        title: pdfInfo.pdf,
        path: `${dirToSave}/${pdfInfo.pdf}`,
    }
    const configOptions = Platform.select({
        ios: {
            fileCache: configfb.fileCache,
            title: configfb.title,
            path: configfb.path,
            appendExt: 'pdf',
        },
        android: configfb,
    });

    console.log('The file saved to 23233', configfb, dirs);

    RNFetchBlob.config(configOptions)
        .fetch('GET', `https://aquatherm.s3.ap-south-1.amazonaws.com/pdfs/${pdfInfo.pdf}`, {})
        .then((res) => {
            if (Platform.OS === "ios") {
                RNFetchBlob.fs.writeFile(configfb.path, res.data, 'base64');
                RNFetchBlob.ios.previewDocument(configfb.path);
            }
            setisdownloaded(false)
            if (Platform.OS == 'android') {
                showSnackbar('File downloaded');
            }
            console.log('The file saved to ', res);
        })
        .catch((e) => {
            setisdownloaded(true)
            showSnackbar(e.message);
            console.log('The file saved to ERROR', e.message)
        });
}
FrankieJLyons commented 3 years ago

@Ajmal0197 How would you go about doing this in a for loop where you're getting multiple files one after another?

Ajmal0197 commented 3 years ago

@Ajmal0197 How would you go about doing this in a for loop where you're getting multiple files one after another?

Use forEach or map i think