itinance / react-native-fs

Native filesystem access for react-native
MIT License
4.96k stars 983 forks source link

How to Downloadfile to /Storage/ folder #889

Open wangfpp opened 4 years ago

wangfpp commented 4 years ago

no such file or directory, open '/storage/emulated/0/Download/a.mp4'

React-native info

System: OS: Linux 5.4 Ubuntu 20.04 LTS (Focal Fossa) CPU: (8) x64 Intel(R) Core(TM) i7-4790 CPU @ 3.60GHz Memory: 689.81 MB / 15.53 GB Shell: 5.0.16 - /bin/bash Binaries: Node: 10.16.2 - /usr/local/bin/node Yarn: 1.16.0 - /usr/local/bin/yarn npm: 6.9.0 - /usr/local/bin/npm SDKs: Android SDK: API Levels: 22, 23, 24, 25, 26, 27, 28, 29 Build Tools: 26.0.2, 28.0.3, 29.0.2 System Images: android-22 | Intel x86 Atom, android-23 | Intel x86 Atom, android-25 | Google APIs ARM 64 v8a, android-27 | Intel x86 Atom, android-27 | Google APIs Intel x86 Atom, android-28 | Google APIs Intel x86 Atom, android-29 | Google Play Intel x86 Atom npmPackages: react: 16.9.0 => 16.9.0 react-native: 0.61.5 => 0.61.5 npmGlobalPackages: react-native-cli: 2.0.1

Mycode

      const downloadDest = `/storage/emulated/0/Download`;
      const options = {
        // fromUrl: videoPath,
        fromUrl: "http://vfx.mtime.cn/Video/2019/03/19/mp4/190319222227698228.mp4",
        toFile: `${downloadDest}/a.mp4`,
        progressDivider: 1,
        begin: (res) => {
          console.warn('begin download', res);
        },
        progress: (res) => {
          console.warn('download progress', res);
          this.setState({
            progress:res
          })
        }
      };
      console.warn(options);
        const ret = RNFS.downloadFile(options);//调用downloadFile开始下载
        ret.promise.then(res => {
          ZkToast.info("下载完成");
        })
        .catch(err => {
          console.warn(err)
        ZkToast.info("下载出错时执行")
        })
hank121314 commented 4 years ago

It seems like that url is invalid. If you want to download file to Android Download Path, you can try to use RNFS. DownloadDirectoryPath.

huurray commented 4 years ago

same isuue.

even if i use RNFS. DownloadDirectoryPath, i've got error 'no such file or directory'. @hank121314

i'm trying this for Android only

 async function handleDownload() {
    const serverPath = `record/${uid}/${date}.aac`;
    const downloadPath = `${RNFS.DownloadDirectoryPath}/${date}.aac`;

    try {
      const url = await storage().ref(serverPath).getDownloadURL();

      console.log(url);
      console.log(downloadPath);
      RNFS.downloadFile({
        fromUrl: url,
        toFile: downloadPath,
        begin: (res) => {
          console.log(11, res);
        },
        progress: () => {
          console.log(22, res);
        },
      });
    } catch (error) {
      console.log(error);
      setDownloadLoading(false);
    }
  }
hank121314 commented 4 years ago

Hi @huurray ! Can you try to add mkdir like this:

async function handleDownload() {
    const serverPath = `record/${uid}/${date}.aac`;
    const downloadPath = `${RNFS.DownloadDirectoryPath}/${date}.aac`;

    try {
      const url = await storage().ref(serverPath).getDownloadURL();

      console.log(url);
      console.log(downloadPath);
      await RNFS.mkdir(downloadPath);
      RNFS.downloadFile({
        fromUrl: url,
        toFile: downloadPath,
        begin: (res) => {
          console.log(11, res);
        },
        progress: () => {
          console.log(22, res);
        },
      });
    } catch (error) {
      console.log(error);
      setDownloadLoading(false);
    }
  }
D-Navadiya commented 3 years ago

Hi, @hank121314

you really saved my day :)

thanks :)