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

Dirs.DocumentDir returning path but not storing files into it. #500

Closed vatsalkgor closed 3 years ago

vatsalkgor commented 4 years ago

Hi I am trying to log some events that my app performs and then email it to concerned person. Below is my code snippet.

let current = new Date();
                RNFetchBlob.fs
                    .writeStream(
                        RNFetchBlob.fs.dirs.DownloadDir +
                            "/" +
                            current +
                            ".txt",
                        "utf8",
                        false
                    )
                    .then(stream => {
                        logsArray.forEach((sentence, index) => {
                            stream.write(sentence + "\r\n");
                        });
                        return stream;
                    })
                    .then(stream => {
                        return stream.close();
                    })
                    .then(() => {
                        logsArray = [];
                        Mailer.mail(
                            {
                                subject:
                                    "TEST EMAIL",
                                recipients: ["email@email.com"],
                                body:
                                    "Hi! I need support for my device. Can you please help? I have attached the logs from the app below.",
                                isHTML: false,
                                attachment: {
                                    path:
                                        RNFetchBlob.fs.dirs.DocumentDir +
                                        "/" +
                                        current +
                                        ".txt",
                                    type: "txt"
                                }
                            },
                            (error, event) => {
                                console.log(error), console.log(event);
                            }
                        );
                    })
                    .catch(err => {
                        console.log(err);
                    });

The problem is if i use the DownloadDir, the file is saved into the download directory in android (DownloadDir not supported in ios). But if I use DocumentDir then the file is not saved in android (not checked in ios)and there's no error occurred as well. I was wondering where I went wrong and wanted some feedbacks. I also want to ask does DocumentDir only works for ios or does it supports android also.

RN VERSION: 0.61.5 rn-fetch-blob: 0.11.2

mohsenzia commented 3 years ago

Same Problem here :(

vatsalkgor commented 3 years ago

Hi @mohsenzia

I ended up doing the following:

let date = new Date();
const dir =
    Platform.OS === "ios"
        ? RNFetchBlob.fs.dirs.DocumentDir + "/" + date + ".txt"
        : RNFetchBlob.fs.dirs.DownloadDir + "/" + date + ".txt";
RNFetchBlob.fs
    .writeStream(
        //Not Sure if DownloadDir works on iOS
        dir,
        "utf8",
        false
    ).then(stream => {
        logsArray.forEach((sentence, index) => {
            stream.write(sentence + "\r\n");
        });
        stream.close();
        return stream;
    })
    .then(() => {
        const path =
            Platform.OS === "ios"
                ? RNFetchBlob.fs.dirs.DocumentDir +
                  "/" +
                  date +
                  ".txt"
                : RNFetchBlob.fs.dirs.DownloadDir +
                  "/" +
                  date +
                  ".txt";
        logsArray = [];
        // send mail logic here
    })