43

我正在使用 React Native 构建一个适用于 Android 和 iOS 的应用程序。我试图让用户在单击按钮时下载 PDF 文件。

  • react-native-file-download不支持安卓
  • 当我触发 downloadFile 时react-native-fs什么都不做(通知栏上没有显示任何内容),之后我无法找到该文件。我添加android.permission.WRITE_EXTERNAL_STORAGE到 Android Manifest 文件中。我仔细检查了我要下载的文件是否存在(如果不存在,库会抛出错误)

我没有找到解决此问题的其他解决方案。我找到了用于查看 PDF 的库,但我想让用户下载 PDF。

4

5 回答 5

51

一个小时前刚刚实现了下载功能:p

按着这些次序:

a) npm install rn-fetch-blob

b) 遵循安装说明。

b2) 如果您想在不使用 rnpm 的情况下手动安装软件包,请访问他们的wiki

c)最后,这就是我在我的应用程序中下载文件的方式:

const { config, fs } = RNFetchBlob
let PictureDir = fs.dirs.PictureDir // this is the pictures directory. You can check the available directories in the wiki.
let options = {
  fileCache: true,
  addAndroidDownloads : {
    useDownloadManager : true, // setting it to true will use the device's native download manager and will be shown in the notification bar.
    notification : false,
    path:  PictureDir + "/me_"+Math.floor(date.getTime() + date.getSeconds() / 2), // this is the path where your downloaded file will live in
    description : 'Downloading image.'
  }
}
config(options).fetch('GET', "http://www.example.com/example.pdf").then((res) => {
  // do some magic here
})
于 2017-06-14T13:56:04.143 回答
25

如果您使用 Expo,react-native-fetch-blob将无法正常工作。使用FileSystem.

这是一个工作示例:

const { uri: localUri } = await FileSystem.downloadAsync(remoteUri, FileSystem.documentDirectory + 'name.ext');

现在您有了localUri下载文件的路径。随意设置您自己的文件名而不是name.ext.

于 2017-09-10T10:07:56.937 回答
3

我遵循了这篇文章上方 Jonathan Simonney 的解决方案。但我不得不稍微改变一下:

const { config, fs } = RNFetchBlob;
  const date = new Date();

  const { DownloadDir } = fs.dirs; // You can check the available directories in the wiki.
  const options = {
    fileCache: true,
    addAndroidDownloads: {
      useDownloadManager: true, // true will use native manager and be shown on notification bar.
      notification: true,
      path: `${DownloadDir}/me_${Math.floor(date.getTime() + date.getSeconds() / 2)}.pdf`,
      description: 'Downloading.',
    },
  };

  config(options).fetch('GET', 'http://www.africau.edu/images/default/sample.pdf').then((res) => {
    console.log('do some magic in here');
  });
于 2021-03-03T19:18:29.633 回答
0

I had the same issue, got it working using Expo WebBrowser Module

// install module 
npm install react-native-webview

// import the module
import * as WebBrowser from 'expo-web-browser';

// then in your function you can call this function
await WebBrowser.openBrowserAsync(file_ur);

it will open preview of the file and then user can download using share button.

于 2020-08-16T22:04:29.647 回答
0
  GetItem_downloadbtn = (item, itemname) => {
    console.log("fiel url comiugn jdd   " + item);
    console.log("item name checkoing " + itemname);
    const android = RNFetchBlob.android;
    const filename = itemname;
    const filepath = RNFetchBlob.fs.dirs.DownloadDir + '/foldernamae/' + filename;
    const downloadAppUrl = item;

    RNFetchBlob.config({
        addAndroidDownloads: {
            useDownloadManager: true,
            title: 'great, download success',
            description:'an apk that will be download',
            mime: 'application/vnd.android.package-archive',
            // mime: 'image/jpeg',
            // mediaScannable: true,
            notification: true,
            path: filepath
        }
    })
    .fetch('GET', downloadAppUrl)
    .then((res) => {
        // console.log('res.path ', res.path());
        alert('res.path ', res.path());
        android.actionViewIntent(res.path(), 'application/vnd.android.package-archive');
    })
    .catch((err) => {
        alert('download error, err is', JSON.stringify(err));
    });
  }
于 2022-01-28T11:14:09.093 回答