【问题标题】:How can i download file from a url in react-native for iOS如何从 iOS 的 react-native 中的 url 下载文件
【发布时间】:2019-01-08 21:00:38
【问题描述】:

我无法在 react-native 中从 url 下载图像,我使用的是“react-native-fetch-blob”,它适用于 android 但不适用于 iOS。 让 PictureDir = fs.dirs.PictureDir; 这是 iOS 未定义的 PicureDir。

代码如下:

const { config, fs } = RNFetchBlob;

let PictureDir = Platform.OS === 'ios' ? fs.dirs.DocumentDir : fs.dirs.PictureDir;
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', "https://images.pexels.com/photos/67636/rose-blue-flower-rose-blooms-67636.jpeg?cs=srgb&dl=beauty-bloom-blue-67636.jpg&fm=jpg").then((res) => {
}) ;

【问题讨论】:

    标签: ios file react-native url download


    【解决方案1】:

    正如文档中所说,PictureDir 仅在 Android 中可用。

    https://github.com/joltup/rn-fetch-blob/wiki/File-System-Access-API#dirs

    DocumentDir
    CacheDir
    MainBundleDir (Can be used to access files embedded on iOS apps only)
    DCIMDir (Android Only)
    DownloadDir (Android Only)
    MusicDir (Android Only)
    PictureDir (Android Only)
    MovieDir (Android Only)
    RingtoneDir (Android Only)
    SDCardDir (0.9.5+ Android Only)
    

    您应该使用DocumentDirCacheDir 在iOS 上保存文件

    你可以做这样的事情,这将允许你根据操作系统使用不同的目录

    import { Platform } from 'react-native';
    
    ...
    
    let PictureDir = Platform.OS === 'ios' ? fs.dirs.DocumentDir : fs.dirs.PictureDir;
    

    更新

    我试用了您更新的代码并且它可以工作,但是我确实必须对其进行一些调整。

    当您使用addAndroidDownloads 时,您必须注意不能为 Android 设置自己的路径。

    在配置中使用 DownloadManager、fileCache 和 path 属性时 不会生效,因为Android DownloadManager只能存储 文件到外部存储,还要注意下载管理器只能 支持 GET 方法,这意味着请求体将被忽略。

    https://github.com/joltup/rn-fetch-blob#android-media-scanner-and-download-manager-support

    所以要设置路径以便 iOS 知道文件的存储位置,您应该在 addAndroidDownloads 对象之外设置路径,以便您的选项变为:

    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,
        description : 'Downloading image.'
      },
      path:  PictureDir + "/me_"+Math.floor(date.getTime() + date.getSeconds() / 2) // this is the path where your downloaded file will live in
    }
    

    对于 iOS,它将忽略 addAndroidDownloads 中的设置,文件将保存在设备上的文档目录中,因为这是给定的路径。

    如果您想将文件保存到 iOS 设备的照片库中,那么您将不得不考虑使用 CameraRoll

    你可以在这里看到我给出的关于如何使用它的答案https://stackoverflow.com/a/54125033/5508175

    【讨论】:

    • 它成功了,我得到了路径,但仍然无法下载文件,我在问题中添加了代码,请看一下。
    • 嘿@Andrew 我正在尝试下载音频文件,但我的应用程序崩溃了这里的代码 sn-p RNFetchBlob.config({ fileCache: true, path: pathFile + '/me_' + Math.floor(date.getTime() + date.getSeconds() / 2), }) .fetch('GET', url) .then(res => { console.log('The file is save to ', res.path()); }) .catch(err => console.log('err', err));
    • @OliverD。我建议将相关部分写成一个新问题,以便您可以清楚地表达您需要做的所有事情,因为 SO 上的 cmets 并不允许进行详细的讨论。一方面,这里的代码格式不好;其次,你被限制在 600 个字符以内,所以很难充分表达自己。
    • 是的.. 很抱歉,我已经这样做了here
    猜你喜欢
    • 2017-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多