Detailed explanation of react-native-fs plug-in usage cases
This time I will bring you a detailed explanation of the usage cases of the react-native-fs plug-in. What are the precautions when using the react-native-fs plug-in? The following is a practical case, let's take a look.
react-native-fs plug-in is used when uploading and downloading files. It can be used on both iOS and android, File upload (iOS only).
Installation command:
npm install react-native-fs --save //注意:如果react native版本是<0.40安装,使用此标签: npm install react-native-fs@2.0.1-rc.2 --save
After installation, execute:
react-native link react-native-fs
In android/app/src/main/AndroidManifest.xml, add android read and write file permissions
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
After completing the above installation operations, you can use various methods of this plug-in. For specific usage examples of each method, please see the link: https://github.com/itinance/react-native-fs. In the project, I need to download the image file, obtain the image path after downloading to the local, and then display the image. So use the downloadFile method. Encapsulates a callable service, the code is as follows:
downloadFile(imageId, cookie, callback) { const downloadDest = `${RNFS.DocumentDirectoryPath}/${((Math.random() * 1000) | 0)}.jpg`; var formUrl = CommonSvc.baseURL + '/api/image/0/' + imageId; //var formUrl = 'http://lorempixel.com/400/200/'; const options = { fromUrl: formUrl, toFile: downloadDest, background: true, headers: { 'Cookie': cookie //需要添加验证到接口要设置cookie }, begin: (res) => { //console.log(res); }, progress: (res) => { //console.log(res); } }; try { const ret = RNFS.downloadFile(options); ret.promise.then(res => { //callback(null, Platform.OS === 'android' ? downloadDest : 'file://' + downloadDest) callback(null, 'file://' + downloadDest) }).catch(err => { callback(err) }); } catch (e) { callback("error") } },
When implementing this function, the pictures downloaded to the local by android cannot be displayed. After consulting the relevant information, the reason is that when android calls this plug-in, it needs to add interface verification information (if the interface needs to be verified) (below), how to solve this problem
When calling the react-native-fs plug-in, if the data interface requires verification information, an error will be reported when running on Android, but there will be no problem when running on iOS. The reason is that the interface has verification information, but it is not passed in when calling this plug-in. On iOS, the verification information will be automatically added, while on Android, it needs to be set manually.
Solution to this error:
1. When calling the login interface, save the cookie (the cookie is in the response), and put it in the headers when calling react-native-fs. The code is as follows:
_appLogin(userName, password, callback){ fetch(commonSvc.baseURL + '/account/app-login', { method: 'POST', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' }, body: JSON.stringify({ UserName: userName, Password: password }) }).then( (response) => { if (response.ok) { return response; } else { var message; switch (response.status) { case 710: message = LanguageChooseSvc.strings['api_common_' + 710]; break; case 711: message = LanguageChooseSvc.strings['api_common_' + 711]; break; case 400: message = LanguageChooseSvc.strings['api_common_' + 400]; break; default: message = commonSvc.httpErrorMessage; break; } throw {message: message}; } } ).then( (responseJson) => { callback(null, responseJson); } ).catch( (error) => { callback(error.message); } ); },
2. When calling react-native-fs, pass it in the headers. The code is as follows:
downloadFile(imageId, cookie, callback) { const downloadDest = `${RNFS.DocumentDirectoryPath}/${((Math.random() * 1000) | 0)}.jpg`; var formUrl = CommonSvc.baseURL + '/api/image/0/' + imageId; //var formUrl = 'http://lorempixel.com/400/200/'; const options = { fromUrl: formUrl, toFile: downloadDest, background: true, headers: { 'Cookie': cookie //需要添加验证到接口要设置cookie }, begin: (res) => { //console.log(res); }, progress: (res) => { //console.log(res); } }; try { const ret = RNFS.downloadFile(options); ret.promise.then(res => { //callback(null, Platform.OS === 'android' ? downloadDest : 'file://' + downloadDest) callback(null, 'file://' + downloadDest) }).catch(err => { callback(err) }); } catch (e) { callback("error") } },
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!
Recommended reading:
js implements character limit Chinese characters = two characters
Using js to implement car dashboard
The above is the detailed content of Detailed explanation of react-native-fs plug-in usage cases. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PyCharm is a powerful and popular Python integrated development environment (IDE) that provides a wealth of functions and tools so that developers can write code more efficiently. The plug-in mechanism of PyCharm is a powerful tool for extending its functions. By installing different plug-ins, various functions and customized features can be added to PyCharm. Therefore, it is crucial for newbies to PyCharm to understand and be proficient in installing plug-ins. This article will give you a detailed introduction to the complete installation of PyCharm plug-in.
![Error loading plugin in Illustrator [Fixed]](https://img.php.cn/upload/article/000/465/014/170831522770626.jpg?x-oss-process=image/resize,m_fill,h_207,w_330)
When launching Adobe Illustrator, does a message about an error loading the plug-in pop up? Some Illustrator users have encountered this error when opening the application. The message is followed by a list of problematic plugins. This error message indicates that there is a problem with the installed plug-in, but it may also be caused by other reasons such as a damaged Visual C++ DLL file or a damaged preference file. If you encounter this error, we will guide you in this article to fix the problem, so continue reading below. Error loading plug-in in Illustrator If you receive an "Error loading plug-in" error message when trying to launch Adobe Illustrator, you can use the following: As an administrator

When users use the Edge browser, they may add some plug-ins to meet more of their needs. But when adding a plug-in, it shows that this plug-in is not supported. How to solve this problem? Today, the editor will share with you three solutions. Come and try it. Method 1: Try using another browser. Method 2: The Flash Player on the browser may be out of date or missing, causing the plug-in to be unsupported. You can download the latest version from the official website. Method 3: Press the "Ctrl+Shift+Delete" keys at the same time. Click "Clear Data" and reopen the browser.

What is the Chrome plug-in extension installation directory? Under normal circumstances, the default installation directory of Chrome plug-in extensions is as follows: 1. The default installation directory location of chrome plug-ins in windowsxp: C:\DocumentsandSettings\username\LocalSettings\ApplicationData\Google\Chrome\UserData\Default\Extensions2. chrome in windows7 The default installation directory location of the plug-in: C:\Users\username\AppData\Local\Google\Chrome\User

Windows operating system is one of the most popular operating systems in the world, and its new version Win11 has attracted much attention. In the Win11 system, obtaining administrator rights is an important operation. Administrator rights allow users to perform more operations and settings on the system. This article will introduce in detail how to obtain administrator permissions in Win11 system and how to effectively manage permissions. In the Win11 system, administrator rights are divided into two types: local administrator and domain administrator. A local administrator has full administrative rights to the local computer

Detailed explanation of division operation in OracleSQL In OracleSQL, division operation is a common and important mathematical operation, used to calculate the result of dividing two numbers. Division is often used in database queries, so understanding the division operation and its usage in OracleSQL is one of the essential skills for database developers. This article will discuss the relevant knowledge of division operations in OracleSQL in detail and provide specific code examples for readers' reference. 1. Division operation in OracleSQL

Does PyCharm Community Edition support enough plugins? Need specific code examples As the Python language becomes more and more widely used in the field of software development, PyCharm, as a professional Python integrated development environment (IDE), is favored by developers. PyCharm is divided into two versions: professional version and community version. The community version is provided for free, but its plug-in support is limited compared to the professional version. So the question is, does PyCharm Community Edition support enough plug-ins? This article will use specific code examples to

Detailed explanation of how to install and set up the EclipseSVN plug-in Eclipse is a widely used integrated development environment (IDE) that supports many different plug-ins to extend its functionality. One of them is the EclipseSVN plugin, which enables developers to interact with the Subversion version control system. This article will detail how to install and set up the EclipseSVN plug-in and provide specific code examples. Step 1: Install the EclipseSVN plug-in and open Eclipse
