react-natvie-fetch-blob使⽤
react-natvie-fetch-blob
⽹上搜索了许多关于react-natvie-fetch-blob 的使⽤介绍,发现中⽂⽂档很少。实在没办法就直接去看github上门的⽂档了,做了⼀些笔记。
英语不怎好,如果写的有何错误之处敬请谅解、指导
我选择react-natvie-fetch-blob的原因
这⾥表述的是个⼈观点,不具备专业性和严谨性
React Native 上传下载的第三发也有挺多的,⽐如react-native-fileupload。对于简单的上传下载也很好⽤,但是功能上还是有⼀些局限性,⽐如上传进度
react-natvie-fetch-blob 功能很强⼤,⽀持进度,还有很多很细⼼的功能
react-natvie-fetch-blob 性能也很好,基本能胜任数据传输的所有功能
特性
react native
传输数据直接存取,⽽不需要base64做跨接
⽂件接⼝⽀持⼀般⽂件,Asset ⽂件,相机胶卷⽂件
Native-to-native ⽂件操作接⼝,减少了JS桥接⽽导致的性能损耗
⽂件流⽀持处理⼤⽂件
Blob, File, XMLHttpRequest polyfills that make browser-based library available in RN (experimental)
⽀持JSON 流(Oboe.js)
相关
这个项⽬的起始出发点是为了解决这个问题
还是看原⽂吧:
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`
安装
1. 安装包,安装完您也可以直接跳到第5步按照wiki教材配置ios和Android环境
npm install --save react-native-fetch-blob
2. 或者如果你使⽤CocoaPods 把下⾯的加⼊到您的Podfile
pod 'react-native-fetch-blob',
:path => '../node_modules/react-native-fetch-blob'
3. ⾃动链接Native Modules
react-native link
4. 使⽤下⾯的命令添加到l ⽂件⾥
RNFB_ANDROID_PERMISSIONS=true react-native link
5. 如果你是⾮默认的项⽬结果,link脚本也许不⽣效。请参考⼿动链接包(也很简单的)
看到这⾥⼩⼩的总结⼀下上⾯五步吧。要不你就1、3、4搞定,要不你就1、5搞定
6. 对于Android 5.0或者更低版本的,授权许可外部扩展存储
Android 6.0发布以来,授权许可机制与之前有了些许改变。有兴趣的同学可以参考,我是没兴趣的!
如果介绍外部存储扩展 (say, SD card storage) for Android 5.0 (or lower) devices,你需要添加下⾯代码的到
<manifest xmlns:android="schemas.android/apk/res/android"
package="fetchblobtest"
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 l
<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>
7. 对于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 l won't work for Android 6.0+ devices. To grant permissions in runtime, you might use PermissionAndroid API.
使⽤
import RNFetchBlob from 'react-native-fetch-blob'

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。