@2hats/react-native-fetch-blob 中文文档教程

发布于 7年前 浏览 24 项目主页 更新于 3年前

react-native-fetch-blob

release< img src="https://img.shields.io/npm/v/react-native-fetch-blob.svg?style=flat-square" alt="npm"> [npm]()

一个致力于制作的项目对于 React Native 开发人员来说,文件访问和数据传输更容易、更高效。

对于 Firebase Storage 解决方案,请升级到最新版本以获得最佳兼容性。

Features

  • Transfer data directly from/to storage without BASE64 bridging
  • File API supports regular files, Asset files, and CameraRoll files
  • Native-to-native file manipulation API, reduce JS bridging performance loss
  • File stream support for dealing with large file
  • Blob, File, XMLHttpRequest polyfills that make browser-based library available in RN (experimental)
  • JSON stream supported base on Oboe.js @jimhigson

TOC (visit Wiki to get the complete documentation)

About

这个项目是为了解决 facebook/react-native#854 问题而启动的,React Native 缺乏Blob 实现在传输二进制数据时会导致问题。

它致力于让 React Native 开发人员更轻松、更高效地访问和传输文件。 我们已经实现了高度可定制的文件系统和网络模块,它们可以很好地协同工作。 例如,开发人员可以直接从存储上传和下载数据,效率更高,尤其是对于大文件。 文件系统支持文件流,访问大文件时不用担心OOM问题。

0.8.0 中,我们引入了实验性 Web API polyfill,使在 React Native 中使用基于浏览器的库成为可能,例如 FireBase JS SDK

Installation

从 npm 安装包

npm install --save react-native-fetch-blob

或者如果使用 CocoaPods,将 pod 添加到 Podfile

pod 'react-native-fetch-blob',
    :path => '../node_modules/react-native-fetch-blob'

0.10.3 之后你可以直接从 Github 安装这个包

# replace <branch_name> with any one of the branches
npm install --save github:wkh237/react-native-fetch-blob-package#<branch_name>

Automatically Link Native Modules

对于 0.29.2+ 项目,只需通过以下命令链接原生包(注意:rnpm 已合并到 react-native 中)

react-native link

至于项目 < ; 0.29 你需要 rnpm 来链接原生包

rnpm link

可选地,使用以下命令将 Android 权限自动添加到 AndroidManifest.xml

RNFB_ANDROID_PERMISSIONS=true react-native link

pre 0.29 项目

RNFB_ANDROID_PERMISSIONS=true rnpm link

如果你有链接脚本可能不会生效非默认项目结构,请访问wiki链接包手动。

为Android 5.0或更低版本授予外部存储权限

Android权限授予机制自Android 6.0发布后略有不同,请参考官方文件

如果您要访问 Android 5.0(或更低版本)设备的外部存储(例如,SD 卡存储),您可能需要将以下行添加到 AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.rnfetchblobtest"
    android:versionCode="1"
    android:versionName="1.0">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
+   <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />                                               
+   <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />                                              

    ...

此外,如果您要使用 Android Download Manager,您必须将此添加到 AndroidManifest.xml

    <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
+           <action android:name="android.intent.action.DOWNLOAD_COMPLETE"/>                          
    </intent-filter>

Grant Access Permission for Android 6.0

从Android 6.0(API 级别 23),用户在应用运行时授予应用权限,而不是在安装应用时。 因此,在 AndroidManifest.xml 中添加权限不适用于 Android 6.0+ 设备。 要在运行时授予权限,您可以使用 PermissionAndroid API

Usage

ES6

模块使用 ES6 风格的 export 语句,只需使用 import 来加载模块。

import RNFetchBlob from 'react-native-fetch-blob'

ES5

如果您使用 ES5 require 语句加载模块,请添加 default。 有关详细信息,请参阅此处

var RNFetchBlob = require('react-native-fetch-blob').default

HTTP Data Transfer

Regular Request

0.8.0 之后,react-native-fetch-blob 通过检查其类型和标头中的 Content-Type 自动决定如何发送主体。 规则如下图描述

总结一下:

  • To send a form data, the Content-Type header does not matter. When the body is an Array we will set proper content type for you.
  • To send binary data, you have two choices, use BASE64 encoded string or path points to a file contains the body.
  • If the Content-Type containing substring;BASE64 or application/octet the given body will be considered as a BASE64 encoded data which will be decoded to binary data as the request body.
  • Otherwise, if a string starts with RNFetchBlob-file:// (which can simply be done by RNFetchBlob.wrap(PATH_TO_THE_FILE)), it will try to find the data from the URI string after RNFetchBlob-file:// and use it as the request body.
  • To send the body as-is, simply use a Content-Type header not containing ;BASE64 or application/octet.

值得一提的是HTTP请求默认使用缓存,如果您要禁用它,只需添加一个 Cache-Control 标头 'Cache-Control' : 'no-store'

在 0.9.4 之后,我们禁用了 Chunked 传输编码默认情况下,如果您要使用它,您应该明确地将标头 Transfer-Encoding 设置为 Chunked

Download example: Fetch files that need authorization token

最简单的方法是下载到内存并存储为 BASE64 编码字符串,这在响应数据较小时很方便。

// send http request in a new thread (using native code)
RNFetchBlob.fetch('GET', 'http://www.example.com/images/img1.png', {
    Authorization : 'Bearer access-token...',
    // more headers  ..
  })
  // when response status code is 200
  .then((res) => {
    // the conversion is done in native code
    let base64Str = res.base64()
    // the following conversions are done in js, it's SYNC
    let text = res.text()
    let json = res.json()

  })
  // Status code is not 200
  .catch((errorMessage, statusCode) => {
    // error handling
  })

Download to storage directly

如果响应数据很大,将其转换为 BASE64 字符串是个坏主意。 更好的解决方案是将响应直接流式传输到文件中,只需将 fileCache 选项添加到配置中,并将其设置为 true。 这将使传入的响应数据存储在一个没有任何文件扩展名的临时路径中。

这些文件不会自动删除,请参考缓存文件管理

RNFetchBlob
  .config({
    // add this option that makes response data to be stored as a file,
    // this is much more performant.
    fileCache : true,
  })
  .fetch('GET', 'http://www.example.com/file/example.zip', {
    //some headers ..
  })
  .then((res) => {
    // the temp file path
    console.log('The file saved to ', res.path())
  })

设置临时文件扩展名

有时您可能出于某种原因需要文件扩展名。 例如,当使用文件路径作为 Image 组件的来源时,路径应该以 .png 或 .jpg 之类的结尾,您可以通过添加 appendExt 选项来做到这一点<代码>配置。

RNFetchBlob
  .config({
    fileCache : true,
    // by adding this option, the temp files will have a file extension
    appendExt : 'png'
  })
  .fetch('GET', 'http://www.example.com/file/example.zip', {
    //some headers ..
  })
  .then((res) => {
    // the temp file path with file extension `png`
    console.log('The file saved to ', res.path())
    // Beware that when using a file path as Image source on Android,
    // you must prepend "file://"" before the file path
    imageView = <Image source={{ uri : Platform.OS === 'android' ? 'file://' + res.path()  : '' + res.path() }}/>
  })

使用特定的文件路径

如果您更喜欢特定的文件路径而不是随机生成的路径,则可以使用path 选项。 我们在 v0.5.0 中添加了几个常量代表常用目录。

let dirs = RNFetchBlob.fs.dirs
RNFetchBlob
.config({
  // response data will be saved to this path if it has access right.
  path : dirs.DocumentDir + '/path-to-file.anything'
})
.fetch('GET', 'http://www.example.com/file/example.zip', {
  //some headers ..
})
.then((res) => {
  // the path should be dirs.DocumentDir + 'path-to-file.anything'
  console.log('The file saved to ', res.path())
})

这些文件不会自动删除,请参考缓存文件管理

Upload example : Dropbox files-upload API

react-native- fetch-blob 将使用本机 API 将 body 中的 base64 字符串转换为二进制格式,此过程在单独的线程中完成,因此它不会阻塞您的 GUI。

RNFetchBlob.fetch('POST', 'https://content.dropboxapi.com/2/files/upload', {
    Authorization : "Bearer access-token...",
    'Dropbox-API-Arg': JSON.stringify({
      path : '/img-from-react-native.png',
      mode : 'add',
      autorename : true,
      mute : false
    }),
    'Content-Type' : 'application/octet-stream',
    // here's the body you're going to send, should be a BASE64 encoded string
    // (you can use "base64"(refer to the library 'mathiasbynens/base64') APIs to make one).
    // The data will be converted to "byte array"(say, blob) before request sent.  
  }, base64ImageString)
  .then((res) => {
    console.log(res.text())
  })
  .catch((err) => {
    // error handling ..
  })

Upload a file from storage

如果您要使用 file 作为请求主体,只需使用 wrap API 包装路径。

RNFetchBlob.fetch('POST', 'https://content.dropboxapi.com/2/files/upload', {
    // dropbox upload headers
    Authorization : "Bearer access-token...",
    'Dropbox-API-Arg': JSON.stringify({
      path : '/img-from-react-native.png',
      mode : 'add',
      autorename : true,
      mute : false
    }),
    'Content-Type' : 'application/octet-stream',
    // Change BASE64 encoded data to a file path with prefix `RNFetchBlob-file://`.
    // Or simply wrap the file path with RNFetchBlob.wrap().
  }, RNFetchBlob.wrap(PATH_TO_THE_FILE))
  .then((res) => {
    console.log(res.text())
  })
  .catch((err) => {
    // error handling ..
  })

Multipart/form-data example: Post form data with file and data

version >= 0.3.0 中,您还可以发布带有表单数据的文件,只需将数组放在 body 中,元素具有属性 namedatafilename(可选)。

具有属性filename 的元素将被转换为二进制格式,否则将转换为utf8 字符串。

  RNFetchBlob.fetch('POST', 'http://www.example.com/upload-form', {
    Authorization : "Bearer access-token",
    otherHeader : "foo",
    'Content-Type' : 'multipart/form-data',
  }, [
    // element with property `filename` will be transformed into `file` in form data
    { name : 'avatar', filename : 'avatar.png', data: binaryDataInBase64},
    // custom content type
    { name : 'avatar-png', filename : 'avatar-png.png', type:'image/png', data: binaryDataInBase64},
    // part file from storage
    { name : 'avatar-foo', filename : 'avatar-foo.png', type:'image/foo', data: RNFetchBlob.wrap(path_to_a_file)},
    // elements without property `filename` will be sent as plain text
    { name : 'name', data : 'user'},
    { name : 'info', data : JSON.stringify({
      mail : 'example@example.com',
      tel : '12345678'
    })},
  ]).then((resp) => {
    // ...
  }).catch((err) => {
    // ...
  })

如果你想附加一个文件来形成数据怎么办? 就像从存储中上传文件示例一样,通过wrapdata code> API(此功能仅适用于 version >= v0.5.0)。 在版本 >= 0.6.2 上,可以在将文件附加到表单数据时设置自定义 MIME 类型。 但请记住,当文件很大时,它可能会使您的应用程序崩溃。 请考虑使用其他策略(参见 #94)。

  RNFetchBlob.fetch('POST', 'http://www.example.com/upload-form', {
    Authorization : "Bearer access-token",
    otherHeader : "foo",
    // this is required, otherwise it won't be process as a multipart/form-data request
    'Content-Type' : 'multipart/form-data',
  }, [
    // append field data from file path
    {
      name : 'avatar',
      filename : 'avatar.png',
      // Change BASE64 encoded data to a file path with prefix `RNFetchBlob-file://`.
      // Or simply wrap the file path with RNFetchBlob.wrap().
      data: RNFetchBlob.wrap(PATH_TO_THE_FILE)
    },
    {
      name : 'ringtone',
      filename : 'ring.mp3',
      // use custom MIME type
      type : 'application/mp3',
      // upload a file from asset is also possible in version >= 0.6.2
      data : RNFetchBlob.wrap(RNFetchBlob.fs.asset('default-ringtone.mp3'))
    }
    // elements without property `filename` will be sent as plain text
    { name : 'name', data : 'user'},
    { name : 'info', data : JSON.stringify({
      mail : 'example@example.com',
      tel : '12345678'
    })},
  ]).then((resp) => {
    // ...
  }).catch((err) => {
    // ...
  })

Upload/Download progress

version >= 0.4.2 中,可以知道上传/下载进度。 0.7.0之后也支持IOS和Android上传进度。

  RNFetchBlob.fetch('POST', 'http://www.example.com/upload', {
      //... some headers,
      'Content-Type' : 'octet-stream'
    }, base64DataString)
    // listen to upload progress event
    .uploadProgress((written, total) => {
        console.log('uploaded', written / total)
    })
    // listen to download progress event
    .progress((received, total) => {
        console.log('progress', received / total)
    })
    .then((resp) => {
      // ...
    })
    .catch((err) => {
      // ...
    })

0.9.6中,你可以指定一个包含countinterval的对象作为第一个参数,用于进度事件的频率(这将在本机上下文中完成以减少 RCT 桥接开销)。 请注意,如果服务器未提供响应内容长度,则 count 参数将不起作用。

  RNFetchBlob.fetch('POST', 'http://www.example.com/upload', {
      //... some headers,
      'Content-Type' : 'octet-stream'
    }, base64DataString)
    // listen to upload progress event, emit every 250ms
    .uploadProgress({ interval : 250 },(written, total) => {
        console.log('uploaded', written / total)
    })
    // listen to download progress event, every 10%
    .progress({ count : 10 }, (received, total) => {
        console.log('progress', received / total)
    })
    .then((resp) => {
      // ...
    })
    .catch((err) => {
      // ...
    })

Cancel Request

0.7.0 之后,可以取消 HTTP 请求。 取消后,它会抛出一个承诺拒绝,一定要抓住它。

let task = RNFetchBlob.fetch('GET', 'http://example.com/file/1')

task.then(() => { ... })
    // handle request cancelled rejection
    .catch((err) => {
        console.log(err)
    })
// cancel the request, the callback function is optional
task.cancel((err) => { ... })

Drop-in Fetch Replacement

0.9.0

如果您有使用whatwg-fetch(官方fetch)的现有代码,则无需将它们替换为RNFetchblob.fetch ,您只需使用我们的 Fetch Replacement。 Official 它们之间的区别是 official fetch 使用 whatwg-fetch ,它在引擎盖下包装了 XMLHttpRequest polyfill。 对于 Web 开发人员来说,它是一个很棒的库,但与 RN 的配合不是很好。 我们的实现只是我们的 fetchfs API 的包装器,因此您可以访问我们提供的所有功能。

查看文档和示例

Android Media Scanner, and Download Manager Support

如果你想在外部存储 在图片、下载或其他内置应用程序中可见,您将不得不使用 Media ScannerDownload Manager

媒体扫描器

媒体扫描器扫描文件并按给定的 MIME 类型分类,如果未指定 MIME 类型,它将尝试使用其文件扩展名解析文件。

RNFetchBlob
    .config({
        // DCIMDir is in external storage
        path : dirs.DCIMDir + '/music.mp3'
    })
    .fetch('GET', 'http://example.com/music.mp3')
    .then((res) => RNFetchBlob.fs.scanFile([ { path : res.path(), mime : 'audio/mpeg' } ]))
    .then(() => {
        // scan file success
    })
    .catch((err) => {
        // scan file error
    })

下载管理器

在 Android 上下载大文件时,推荐使用 下载管理器,它支持许多原生功能,如进度条和通知,下载任务也会由操作系统处理,效率更高。

使用 DownloadManager 时,config 中的 fileCachepath 属性不会生效,因为Android DownloadManager只能存储文件到外部存储,还要注意DownloadManager只能支持GET方法,这意味着请求体将被忽略。

下载完成后,DownloadManager 会生成一个文件路径,方便您进行处理。

RNFetchBlob
    .config({
        addAndroidDownloads : {
            useDownloadManager : true, // <-- this is the only thing required
            // Optional, override notification setting (default to true)
            notification : false,
            // Optional, but recommended since android DownloadManager will fail when
            // the url does not contains a file extension, by default the mime type will be text/plain
            mime : 'text/plain',
            description : 'File downloaded by download manager.'
        }
    })
    .fetch('GET', 'http://example.com/file/somefile')
    .then((resp) => {
      // the path of downloaded file
      resp.path()
    })

您的应用可能无权删除/更改下载管理器创建的文件,因此您可能需要 为下载任务设置自定义位置

下载应用程序中的下载通知和可见性(仅限 Android)

如果您需要在文件下载到存储时显示通知(如上所述)或使下载的文件在“下载”应用程序中可见。 您必须向 config 添加一些选项。

RNFetchBlob.config({
  fileCache : true,
  // android only options, these options be a no-op on IOS
  addAndroidDownloads : {
    // Show notification when response data transmitted
    notification : true,
    // Title of download notification
    title : 'Great ! Download Success ! :O ',
    // File description (not notification description)
    description : 'An image file.',
    mime : 'image/png',
    // Make the file scannable  by media scanner
    mediaScannable : true,
  }
})
.fetch('GET', 'http://example.com/image1.png')
.then(...)

使用 Intent 打开下载的文件

如果您要使用官方 Linking API 可能无法按预期工作,另外,如果您要在 Downloads 中安装 APK应用程序,那也不会起作用。 作为替代方案,您可以尝试使用 actionViewIntent API,它会为您发送一个使用给定 MIME 类型的 ACTION_VIEW 意图。

以编程方式下载并安装 APK

const android = RNFetchBlob.android

RNFetchBlob.config({
    addAndroidDownloads : {
      useDownloadManager : true,
      title : 'awesome.apk',
      description : 'An APK that will be installed',
      mime : 'application/vnd.android.package-archive',
      mediaScannable : true,
      notification : true,
    }
  })
  .fetch('GET', `http://www.example.com/awesome.apk`)
  .then((res) => {
      android.actionViewIntent(res.path(), 'application/vnd.android.package-archive')
  })

或在图像查看器中显示图像

      android.actionViewIntent(PATH_OF_IMG, 'image/png')

File System

File Access

文件访问 API 是在开发 v0.5.0 时创建的,这有助于我们编写测试,并且不打算成为该模块的一部分。 然而,我们意识到很难找到一个很好的解决方案来管理缓存文件,每个使用这个模块的人都可能需要这些 API 来处理他们的情况。

在开始使用文件 API 之前,我们建议阅读 首先是文件源之间的差异

文件访问 API

有关详细信息,请参阅 File API

File Stream

v0.5.0 我们添加了 writeStreamreadStream,它们允许您的应用从文件路径读取/写入数据。 此 API 创建一个文件流,而不是将整个数据转换为 BASE64 编码的字符串。 它在处理大文件时很方便。

当调用readStream 方法时,您必须打开 流,并开始读取数据。 当文件很大时,考虑使用适当的 bufferSizeinterval 来减少本机事件调度开销(参见 性能提示)

文件流事件有一个默认的节流阀(10ms) 和缓冲区大小,以防止它对主线程造成过多的开销,你也可以 调整这些值

let data = ''
RNFetchBlob.fs.readStream(
    // file path
    PATH_TO_THE_FILE,
    // encoding, should be one of `base64`, `utf8`, `ascii`
    'base64',
    // (optional) buffer size, default to 4096 (4095 for BASE64 encoded data)
    // when reading file in BASE64 encoding, buffer size must be multiples of 3.
    4095)
.then((ifstream) => {
    ifstream.open()
    ifstream.onData((chunk) => {
      // when encoding is `ascii`, chunk will be an array contains numbers
      // otherwise it will be a string
      data += chunk
    })
    ifstream.onError((err) => {
      console.log('oops', err)
    })
    ifstream.onEnd(() => {  
      <Image source={{ uri : 'data:image/png,base64' + data }}
    })
})

使用 writeStream 时,流对象变为可写,然后您可以执行 writeclose 等操作。

RNFetchBlob.fs.writeStream(
    PATH_TO_FILE,
    // encoding, should be one of `base64`, `utf8`, `ascii`
    'utf8',
    // should data append to existing content ?
    true)
.then((ofstream) => {
    ofstream.write('foo')
    ofstream.write('bar')
    ofstream.close()
})

Cache File Management

当使用 fileCachepath 选项以及 fetch API 时,响应数据将自动存储到文件系统中。 这些文件将不会删除,除非您取消链接它。 有多种方法可以删除文件

  // remove file using RNFetchblobResponse.flush() object method
  RNFetchblob.config({
      fileCache : true
    })
    .fetch('GET', 'http://example.com/download/file')
    .then((res) => {
      // remove cached file from storage
      res.flush()
    })

  // remove file by specifying a path
  RNFetchBlob.fs.unlink('some-file-path').then(() => {
    // ...
  })

您还可以使用 session API 对请求进行分组,并在需要时使用 dispose 将它们全部删除。

  RNFetchblob.config({
    fileCache : true
  })
  .fetch('GET', 'http://example.com/download/file')
  .then((res) => {
    // set session of a response
    res.session('foo')
  })  

  RNFetchblob.config({
    // you can also set session beforehand
    session : 'foo'
    fileCache : true
  })
  .fetch('GET', 'http://example.com/download/file')
  .then((res) => {
    // ...
  })  

  // or put an existing file path to the session
  RNFetchBlob.session('foo').add('some-file-path')
  // remove a file path from the session
  RNFetchBlob.session('foo').remove('some-file-path')
  // list paths of a session
  RNFetchBlob.session('foo').list()
  // remove all files in a session
  RNFetchBlob.session('foo').dispose().then(() => { ... })

Transfer Encoding

0.9.4 之后,由于某些服务提供商可能不支持分块传输,因此默认禁用 Chunked 传输编码。 要启用它,请将 Transfer-Encoding 标头设置为 Chunked

RNFetchBlob.fetch('POST', 'http://example.com/upload', { 'Transfer-Encoding' : 'Chunked' }, bodyData)

Self-Signed SSL Server

默认情况下,react-native-fetch-blob 不允许连接到未知的证书提供者,因为它很危险。 要连接具有自签名证书的服务器,您需要将 trusty 添加到 config 中。 此功能适用于版本 >= 0.5.3

RNFetchBlob.config({
  trusty : true
})
.then('GET', 'https://mysite.com')
.then((resp) => {
  // ...
})

Web API Polyfills

0.8.0 之后我们制作了一些 Web API polyfills 使一些基于浏览器的库在 RN 中可用。

  • Blob
  • XMLHttpRequest (Use our implementation if you're going to use it with Blob)

这是一个使用 polyfill 将文件上传到 FireBase 的示例应用

Performance Tips

Read Stream 和 Progress Event Overhead

如果通过 fs.readStream 读取数据时,如果文件很大,进程似乎阻塞了 JS 线程。 这可能是因为默认缓冲区大小非常小(4kb),导致从 JS 线程触发了很多事件。 尝试增加缓冲区大小(例如100kb = 102400)并设置更大的间隔(适用于0.9.4+,默认值为10ms)以限制频率。

减少 RCT Bridge 和 BASE64 开销

React Native 通过在 React Native 桥周围传递 JSON 来连接 JS 和 Native 上下文,并且在将数据发送到每一方之前会产生转换数据的开销。 当数据很大时,这会对您的应用程序产生相当大的性能影响。 如果可能,建议使用文件存储而不是 BASE64。下图显示了在 iPhone 6 上从存储加载数据比 BASE64 编码字符串快多少。

ASCII 编码具有/糟糕的性能

由于 JavascriptCore 中缺少类型化数组实现, React Native 结构的局限性,将数据转换为 JS 字节数组会花费大量时间。 仅在需要时使用它,下表显示了读取具有不同编码的文件所花费的时间。

连接和替换文件

如果你要连接文件,你不必读取数据到JS 上下文了! 在 0.8.0 中,我们为 writeFile 和 appendFile API 引入了新的编码 uri,这使得在原生中处理整个过程成为可能。

Caveats

  • This library does not urlencode unicode characters in URL automatically, see #146.
  • When you create a Blob , from an existing file, the file WILL BE REMOVED if you close the blob.
  • If you replaced window.XMLHttpRequest for some reason (e.g. make Firebase SDK work), it will also affect how official fetch works (basically it should work just fine).
  • When file stream and upload/download progress event slow down your app, consider an upgrade to 0.9.6+, use additional arguments to limit its frequency.
  • When passing a file path to the library, remove file:// prefix.

当你遇到问题时,看看 故障排除标记为故障排除的问题,会有一些有用的信息。

Changes

请参阅发行说明

Development

如果您有兴趣破解此模块,请查看我们的开发指南,可能会有一些有用的信息。 请随时提出 PR 或提出问题。

react-native-fetch-blob

releasenpm [npm]()

A project committed to making file access and data transfer easier and more efficient for React Native developers.

For Firebase Storage solution, please upgrade to the latest version for the best compatibility.

Features

  • Transfer data directly from/to storage without BASE64 bridging
  • File API supports regular files, Asset files, and CameraRoll files
  • Native-to-native file manipulation API, reduce JS bridging performance loss
  • File stream support for dealing with large file
  • Blob, File, XMLHttpRequest polyfills that make browser-based library available in RN (experimental)
  • JSON stream supported base on Oboe.js @jimhigson

TOC (visit Wiki to get the complete documentation)

About

This project was started in the cause of solving issue facebook/react-native#854, React Native's lacks of Blob implementation which results into problems when transferring binary data.

It is committed to making file access and transfer easier and more efficient for React Native developers. We've implemented highly customizable filesystem and network module which plays well together. For example, developers can upload and download data directly from/to storage, which is more efficient, especially for large files. The file system supports file stream, so you don't have to worry about OOM problem when accessing large files.

In 0.8.0 we introduced experimental Web API polyfills that make it possible to use browser-based libraries in React Native, such as, FireBase JS SDK

Installation

Install package from npm

npm install --save react-native-fetch-blob

Or if using CocoaPods, add the pod to your Podfile

pod 'react-native-fetch-blob',
    :path => '../node_modules/react-native-fetch-blob'

After 0.10.3 you can install this package directly from Github

# replace <branch_name> with any one of the branches
npm install --save github:wkh237/react-native-fetch-blob-package#<branch_name>

Automatically Link Native Modules

For 0.29.2+ projects, simply link native packages via the following command (note: rnpm has been merged into react-native)

react-native link

As for projects < 0.29 you need rnpm to link native packages

rnpm link

Optionally, use the following command to add Android permissions to AndroidManifest.xml automatically

RNFB_ANDROID_PERMISSIONS=true react-native link

pre 0.29 projects

RNFB_ANDROID_PERMISSIONS=true rnpm link

The link script might not take effect if you have non-default project structure, please visit the wiki to link the package manually.

Grant Permission to External storage for Android 5.0 or lower

The mechanism for granting Android permissions has slightly different since Android 6.0 released, please refer to Official Document.

If you're going to access external storage (say, SD card storage) for Android 5.0 (or lower) devices, you might have to add the following line to AndroidManifest.xml.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.rnfetchblobtest"
    android:versionCode="1"
    android:versionName="1.0">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
+   <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />                                               
+   <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />                                              

    ...

Also, if you're going to use Android Download Manager you have to add this to AndroidManifest.xml

    <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
+           <action android:name="android.intent.action.DOWNLOAD_COMPLETE"/>                          
    </intent-filter>

Grant Access Permission for Android 6.0

Beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app. So adding permissions in AndroidManifest.xml won't work for Android 6.0+ devices. To grant permissions in runtime, you might use PermissionAndroid API.

Usage

ES6

The module uses ES6 style export statement, simply use import to load the module.

import RNFetchBlob from 'react-native-fetch-blob'

ES5

If you're using ES5 require statement to load the module, please add default. See here for more detail.

var RNFetchBlob = require('react-native-fetch-blob').default

HTTP Data Transfer

Regular Request

After 0.8.0 react-native-fetch-blob automatically decides how to send the body by checking its type and Content-Type in the header. The rule is described in the following diagram

To sum up:

  • To send a form data, the Content-Type header does not matter. When the body is an Array we will set proper content type for you.
  • To send binary data, you have two choices, use BASE64 encoded string or path points to a file contains the body.
  • If the Content-Type containing substring;BASE64 or application/octet the given body will be considered as a BASE64 encoded data which will be decoded to binary data as the request body.
  • Otherwise, if a string starts with RNFetchBlob-file:// (which can simply be done by RNFetchBlob.wrap(PATH_TO_THE_FILE)), it will try to find the data from the URI string after RNFetchBlob-file:// and use it as the request body.
  • To send the body as-is, simply use a Content-Type header not containing ;BASE64 or application/octet.

It is Worth to mentioning that the HTTP request uses cache by default, if you're going to disable it simply add a Cache-Control header 'Cache-Control' : 'no-store'

After 0.9.4, we disabled Chunked transfer encoding by default, if you're going to use it, you should explicitly set header Transfer-Encoding to Chunked.

Download example: Fetch files that need authorization token

Most simple way is download to memory and stored as BASE64 encoded string, this is handy when the response data is small.

// send http request in a new thread (using native code)
RNFetchBlob.fetch('GET', 'http://www.example.com/images/img1.png', {
    Authorization : 'Bearer access-token...',
    // more headers  ..
  })
  // when response status code is 200
  .then((res) => {
    // the conversion is done in native code
    let base64Str = res.base64()
    // the following conversions are done in js, it's SYNC
    let text = res.text()
    let json = res.json()

  })
  // Status code is not 200
  .catch((errorMessage, statusCode) => {
    // error handling
  })

Download to storage directly

If the response data is large, that would be a bad idea to convert it into BASE64 string. A better solution is streaming the response directly into a file, simply add a fileCache option to config, and set it to true. This will make incoming response data stored in a temporary path without any file extension.

These files won't be removed automatically, please refer to Cache File Management

RNFetchBlob
  .config({
    // add this option that makes response data to be stored as a file,
    // this is much more performant.
    fileCache : true,
  })
  .fetch('GET', 'http://www.example.com/file/example.zip', {
    //some headers ..
  })
  .then((res) => {
    // the temp file path
    console.log('The file saved to ', res.path())
  })

Set Temp File Extension

Sometimes you might need a file extension for some reason. For example, when using file path as the source of Image component, the path should end with something like .png or .jpg, you can do this by add appendExt option to config.

RNFetchBlob
  .config({
    fileCache : true,
    // by adding this option, the temp files will have a file extension
    appendExt : 'png'
  })
  .fetch('GET', 'http://www.example.com/file/example.zip', {
    //some headers ..
  })
  .then((res) => {
    // the temp file path with file extension `png`
    console.log('The file saved to ', res.path())
    // Beware that when using a file path as Image source on Android,
    // you must prepend "file://"" before the file path
    imageView = <Image source={{ uri : Platform.OS === 'android' ? 'file://' + res.path()  : '' + res.path() }}/>
  })

Use Specific File Path

If you prefer a particular file path rather than randomly generated one, you can use path option. We've added several constants in v0.5.0 which represents commonly used directories.

let dirs = RNFetchBlob.fs.dirs
RNFetchBlob
.config({
  // response data will be saved to this path if it has access right.
  path : dirs.DocumentDir + '/path-to-file.anything'
})
.fetch('GET', 'http://www.example.com/file/example.zip', {
  //some headers ..
})
.then((res) => {
  // the path should be dirs.DocumentDir + 'path-to-file.anything'
  console.log('The file saved to ', res.path())
})

These files won't be removed automatically, please refer to Cache File Management

Upload example : Dropbox files-upload API

react-native-fetch-blob will convert the base64 string in body to binary format using native API, this process is done in a separated thread so that it won't block your GUI.

RNFetchBlob.fetch('POST', 'https://content.dropboxapi.com/2/files/upload', {
    Authorization : "Bearer access-token...",
    'Dropbox-API-Arg': JSON.stringify({
      path : '/img-from-react-native.png',
      mode : 'add',
      autorename : true,
      mute : false
    }),
    'Content-Type' : 'application/octet-stream',
    // here's the body you're going to send, should be a BASE64 encoded string
    // (you can use "base64"(refer to the library 'mathiasbynens/base64') APIs to make one).
    // The data will be converted to "byte array"(say, blob) before request sent.  
  }, base64ImageString)
  .then((res) => {
    console.log(res.text())
  })
  .catch((err) => {
    // error handling ..
  })

Upload a file from storage

If you're going to use a file as request body, just wrap the path with wrap API.

RNFetchBlob.fetch('POST', 'https://content.dropboxapi.com/2/files/upload', {
    // dropbox upload headers
    Authorization : "Bearer access-token...",
    'Dropbox-API-Arg': JSON.stringify({
      path : '/img-from-react-native.png',
      mode : 'add',
      autorename : true,
      mute : false
    }),
    'Content-Type' : 'application/octet-stream',
    // Change BASE64 encoded data to a file path with prefix `RNFetchBlob-file://`.
    // Or simply wrap the file path with RNFetchBlob.wrap().
  }, RNFetchBlob.wrap(PATH_TO_THE_FILE))
  .then((res) => {
    console.log(res.text())
  })
  .catch((err) => {
    // error handling ..
  })

Multipart/form-data example: Post form data with file and data

In version >= 0.3.0 you can also post files with form data, just put an array in body, with elements have property name, data, and filename(optional).

Elements have property filename will be transformed into binary format, otherwise, it turns into utf8 string.

  RNFetchBlob.fetch('POST', 'http://www.example.com/upload-form', {
    Authorization : "Bearer access-token",
    otherHeader : "foo",
    'Content-Type' : 'multipart/form-data',
  }, [
    // element with property `filename` will be transformed into `file` in form data
    { name : 'avatar', filename : 'avatar.png', data: binaryDataInBase64},
    // custom content type
    { name : 'avatar-png', filename : 'avatar-png.png', type:'image/png', data: binaryDataInBase64},
    // part file from storage
    { name : 'avatar-foo', filename : 'avatar-foo.png', type:'image/foo', data: RNFetchBlob.wrap(path_to_a_file)},
    // elements without property `filename` will be sent as plain text
    { name : 'name', data : 'user'},
    { name : 'info', data : JSON.stringify({
      mail : 'example@example.com',
      tel : '12345678'
    })},
  ]).then((resp) => {
    // ...
  }).catch((err) => {
    // ...
  })

What if you want to append a file to form data? Just like upload a file from storage example, wrap data by wrap API (this feature is only available for version >= v0.5.0). On version >= 0.6.2, it is possible to set custom MIME type when appending a file to form data. But keep in mind when the file is large it's likely to crash your app. Please consider use other strategy (see #94).

  RNFetchBlob.fetch('POST', 'http://www.example.com/upload-form', {
    Authorization : "Bearer access-token",
    otherHeader : "foo",
    // this is required, otherwise it won't be process as a multipart/form-data request
    'Content-Type' : 'multipart/form-data',
  }, [
    // append field data from file path
    {
      name : 'avatar',
      filename : 'avatar.png',
      // Change BASE64 encoded data to a file path with prefix `RNFetchBlob-file://`.
      // Or simply wrap the file path with RNFetchBlob.wrap().
      data: RNFetchBlob.wrap(PATH_TO_THE_FILE)
    },
    {
      name : 'ringtone',
      filename : 'ring.mp3',
      // use custom MIME type
      type : 'application/mp3',
      // upload a file from asset is also possible in version >= 0.6.2
      data : RNFetchBlob.wrap(RNFetchBlob.fs.asset('default-ringtone.mp3'))
    }
    // elements without property `filename` will be sent as plain text
    { name : 'name', data : 'user'},
    { name : 'info', data : JSON.stringify({
      mail : 'example@example.com',
      tel : '12345678'
    })},
  ]).then((resp) => {
    // ...
  }).catch((err) => {
    // ...
  })

Upload/Download progress

In version >= 0.4.2 it is possible to know the upload/download progress. After 0.7.0 IOS and Android upload progress are also supported.

  RNFetchBlob.fetch('POST', 'http://www.example.com/upload', {
      //... some headers,
      'Content-Type' : 'octet-stream'
    }, base64DataString)
    // listen to upload progress event
    .uploadProgress((written, total) => {
        console.log('uploaded', written / total)
    })
    // listen to download progress event
    .progress((received, total) => {
        console.log('progress', received / total)
    })
    .then((resp) => {
      // ...
    })
    .catch((err) => {
      // ...
    })

In 0.9.6, you can specify an object as the first argument which contains count and interval, to the frequency of progress event (this will be done in the native context a reduce RCT bridge overhead). Notice that count argument will not work if the server does not provide response content length.

  RNFetchBlob.fetch('POST', 'http://www.example.com/upload', {
      //... some headers,
      'Content-Type' : 'octet-stream'
    }, base64DataString)
    // listen to upload progress event, emit every 250ms
    .uploadProgress({ interval : 250 },(written, total) => {
        console.log('uploaded', written / total)
    })
    // listen to download progress event, every 10%
    .progress({ count : 10 }, (received, total) => {
        console.log('progress', received / total)
    })
    .then((resp) => {
      // ...
    })
    .catch((err) => {
      // ...
    })

Cancel Request

After 0.7.0 it is possible to cancel an HTTP request. Upon cancellation, it throws a promise rejection, be sure to catch it.

let task = RNFetchBlob.fetch('GET', 'http://example.com/file/1')

task.then(() => { ... })
    // handle request cancelled rejection
    .catch((err) => {
        console.log(err)
    })
// cancel the request, the callback function is optional
task.cancel((err) => { ... })

Drop-in Fetch Replacement

0.9.0

If you have existing code that uses whatwg-fetch(the official fetch), it's not necessary to replace them with RNFetchblob.fetch, you can simply use our Fetch Replacement. The difference between Official them is official fetch uses whatwg-fetch which wraps XMLHttpRequest polyfill under the hood. It's a great library for web developers, but does not play very well with RN. Our implementation is simply a wrapper of our fetch and fs APIs, so you can access all the features we provided.

See document and examples

Android Media Scanner, and Download Manager Support

If you want to make a file in External Storage becomes visible in Picture, Downloads, or other built-in apps, you will have to use Media Scanner or Download Manager.

Media Scanner

Media scanner scans the file and categorizes by given MIME type, if MIME type not specified, it will try to resolve the file using its file extension.

RNFetchBlob
    .config({
        // DCIMDir is in external storage
        path : dirs.DCIMDir + '/music.mp3'
    })
    .fetch('GET', 'http://example.com/music.mp3')
    .then((res) => RNFetchBlob.fs.scanFile([ { path : res.path(), mime : 'audio/mpeg' } ]))
    .then(() => {
        // scan file success
    })
    .catch((err) => {
        // scan file error
    })

Download Manager

When downloading large files on Android it is recommended to use Download Manager, it supports a lot of native features like the progress bar, and notification, also the download task will be handled by OS, and more efficient.

When using DownloadManager, fileCache and path properties in config will not take effect, because Android DownloadManager can only store files to external storage, also notice that Download Manager can only support GET method, which means the request body will be ignored.

When download complete, DownloadManager will generate a file path so that you can deal with it.

RNFetchBlob
    .config({
        addAndroidDownloads : {
            useDownloadManager : true, // <-- this is the only thing required
            // Optional, override notification setting (default to true)
            notification : false,
            // Optional, but recommended since android DownloadManager will fail when
            // the url does not contains a file extension, by default the mime type will be text/plain
            mime : 'text/plain',
            description : 'File downloaded by download manager.'
        }
    })
    .fetch('GET', 'http://example.com/file/somefile')
    .then((resp) => {
      // the path of downloaded file
      resp.path()
    })

Your app might not have right to remove/change the file created by Download Manager, therefore you might need to set custom location to the download task.

Download Notification and Visibility in Download App (Android Only)

If you need to display a notification upon the file is downloaded to storage (as the above) or make the downloaded file visible in "Downloads" app. You have to add some options to config.

RNFetchBlob.config({
  fileCache : true,
  // android only options, these options be a no-op on IOS
  addAndroidDownloads : {
    // Show notification when response data transmitted
    notification : true,
    // Title of download notification
    title : 'Great ! Download Success ! :O ',
    // File description (not notification description)
    description : 'An image file.',
    mime : 'image/png',
    // Make the file scannable  by media scanner
    mediaScannable : true,
  }
})
.fetch('GET', 'http://example.com/image1.png')
.then(...)

Open Downloaded File with Intent

This is a new feature added in 0.9.0 if you're going to open a file path using official Linking API that might not work as expected, also, if you're going to install an APK in Downloads app, that will not function too. As an alternative, you can try actionViewIntent API, which will send an ACTION_VIEW intent for you which uses the given MIME type.

Download and install an APK programmatically

const android = RNFetchBlob.android

RNFetchBlob.config({
    addAndroidDownloads : {
      useDownloadManager : true,
      title : 'awesome.apk',
      description : 'An APK that will be installed',
      mime : 'application/vnd.android.package-archive',
      mediaScannable : true,
      notification : true,
    }
  })
  .fetch('GET', `http://www.example.com/awesome.apk`)
  .then((res) => {
      android.actionViewIntent(res.path(), 'application/vnd.android.package-archive')
  })

Or show an image in image viewer

      android.actionViewIntent(PATH_OF_IMG, 'image/png')

File System

File Access

File access APIs were made when developing v0.5.0, which helping us write tests, and was not planned to be a part of this module. However, we realized that it's hard to find a great solution to manage cached files, everyone who uses this module may need these APIs for their cases.

Before start using file APIs, we recommend read Differences between File Source first.

File Access APIs

See File API for more information

File Stream

In v0.5.0 we've added writeStream and readStream, which allows your app read/write data from the file path. This API creates a file stream, rather than convert entire data into BASE64 encoded string. It's handy when processing large files.

When calling readStream method, you have to open the stream, and start to read data. When the file is large, consider using an appropriate bufferSize and interval to reduce the native event dispatching overhead (see Performance Tips)

The file stream event has a default throttle(10ms) and buffer size which preventing it cause too much overhead to main thread, yo can also tweak these values.

let data = ''
RNFetchBlob.fs.readStream(
    // file path
    PATH_TO_THE_FILE,
    // encoding, should be one of `base64`, `utf8`, `ascii`
    'base64',
    // (optional) buffer size, default to 4096 (4095 for BASE64 encoded data)
    // when reading file in BASE64 encoding, buffer size must be multiples of 3.
    4095)
.then((ifstream) => {
    ifstream.open()
    ifstream.onData((chunk) => {
      // when encoding is `ascii`, chunk will be an array contains numbers
      // otherwise it will be a string
      data += chunk
    })
    ifstream.onError((err) => {
      console.log('oops', err)
    })
    ifstream.onEnd(() => {  
      <Image source={{ uri : 'data:image/png,base64' + data }}
    })
})

When using writeStream, the stream object becomes writable, and you can then perform operations like write and close.

RNFetchBlob.fs.writeStream(
    PATH_TO_FILE,
    // encoding, should be one of `base64`, `utf8`, `ascii`
    'utf8',
    // should data append to existing content ?
    true)
.then((ofstream) => {
    ofstream.write('foo')
    ofstream.write('bar')
    ofstream.close()
})

Cache File Management

When using fileCache or path options along with fetch API, response data will automatically store into the file system. The files will NOT removed unless you unlink it. There're several ways to remove the files

  // remove file using RNFetchblobResponse.flush() object method
  RNFetchblob.config({
      fileCache : true
    })
    .fetch('GET', 'http://example.com/download/file')
    .then((res) => {
      // remove cached file from storage
      res.flush()
    })

  // remove file by specifying a path
  RNFetchBlob.fs.unlink('some-file-path').then(() => {
    // ...
  })

You can also group requests by using session API and use dispose to remove them all when needed.

  RNFetchblob.config({
    fileCache : true
  })
  .fetch('GET', 'http://example.com/download/file')
  .then((res) => {
    // set session of a response
    res.session('foo')
  })  

  RNFetchblob.config({
    // you can also set session beforehand
    session : 'foo'
    fileCache : true
  })
  .fetch('GET', 'http://example.com/download/file')
  .then((res) => {
    // ...
  })  

  // or put an existing file path to the session
  RNFetchBlob.session('foo').add('some-file-path')
  // remove a file path from the session
  RNFetchBlob.session('foo').remove('some-file-path')
  // list paths of a session
  RNFetchBlob.session('foo').list()
  // remove all files in a session
  RNFetchBlob.session('foo').dispose().then(() => { ... })

Transfer Encoding

After 0.9.4, the Chunked transfer encoding is disabled by default due to some service provider may not support chunked transfer. To enable it, set Transfer-Encoding header to Chunked.

RNFetchBlob.fetch('POST', 'http://example.com/upload', { 'Transfer-Encoding' : 'Chunked' }, bodyData)

Self-Signed SSL Server

By default, react-native-fetch-blob does NOT allow connection to unknown certification provider since it's dangerous. To connect a server with self-signed certification, you need to add trusty to config explicitly. This function is available for version >= 0.5.3

RNFetchBlob.config({
  trusty : true
})
.then('GET', 'https://mysite.com')
.then((resp) => {
  // ...
})

Web API Polyfills

After 0.8.0 we've made some Web API polyfills that makes some browser-based library available in RN.

  • Blob
  • XMLHttpRequest (Use our implementation if you're going to use it with Blob)

Here's a sample app that uses polyfills to upload files to FireBase.

Performance Tips

Read Stream and Progress Event Overhead

If the process seems to block JS thread when file is large when reading data via fs.readStream. It might because the default buffer size is quite small (4kb) which result in a lot of events triggered from JS thread. Try to increase the buffer size (for example 100kb = 102400) and set a larger interval (available for 0.9.4+, the default value is 10ms) to limit the frequency.

Reduce RCT Bridge and BASE64 Overhead

React Native connects JS and Native context by passing JSON around React Native bridge, and there will be an overhead to convert data before they sent to each side. When data is large, this will be quite a performance impact to your app. It's recommended to use file storage instead of BASE64 if possible.The following chart shows how much faster when loading data from storage than BASE64 encoded string on iPhone 6.

ASCII Encoding has /terrible Performance

Due to the lack of typed array implementation in JavascriptCore, and limitation of React Native structure, to convert data to JS byte array spends lot of time. Use it only when needed, the following chart shows how much time it takes when reading a file with different encoding.

Concat and Replacing Files

If you're going to concatenate files, you don't have to read the data to JS context anymore! In 0.8.0 we introduced new encoding uri for writeFile and appendFile API, which make it possible to handle the whole process in native.

Caveats

  • This library does not urlencode unicode characters in URL automatically, see #146.
  • When you create a Blob , from an existing file, the file WILL BE REMOVED if you close the blob.
  • If you replaced window.XMLHttpRequest for some reason (e.g. make Firebase SDK work), it will also affect how official fetch works (basically it should work just fine).
  • When file stream and upload/download progress event slow down your app, consider an upgrade to 0.9.6+, use additional arguments to limit its frequency.
  • When passing a file path to the library, remove file:// prefix.

when you got a problem, have a look at Trouble Shooting or issues labeled Trouble Shooting, there'd be some helpful information.

Changes

See release notes

Development

If you're interested in hacking this module, check our development guide, there might be some helpful information. Please feel free to make a PR or file an issue.

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文