{"id":527,"date":"2023-02-25T15:49:00","date_gmt":"2023-02-25T07:49:00","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=527"},"modified":"2023-04-29T20:42:14","modified_gmt":"2023-04-29T12:42:14","slug":"react-native-file-system-and-file-upload-download-component-react-native-fs-compatible-with-android-ios","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/25\/react-native-file-system-and-file-upload-download-component-react-native-fs-compatible-with-android-ios\/","title":{"rendered":"React Native\u6587\u4ef6\u7cfb\u7edf\u4e0e\u6587\u4ef6\u4e0a\u4f20\u4e0b\u8f7d\u7ec4\u4ef6react-native-fs\uff08\u9002\u914dAndroid &#038; iOS\uff09"},"content":{"rendered":"<p>\u53ef\u4ee5\u5b9e\u73b0\u6587\u4ef6\u7684\u521b\u5efa\u3001\u5220\u9664\u3001\u590d\u5236\u3001\u79fb\u52a8\u3001\u4e0b\u8f7d\u3001\u4e0a\u4f20\u7b49\u529f\u80fd\u3002<\/p>\n<p>\u9879\u76ee\u5730\u5740\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/github.com\/johanneslumpe\/react-native-fs\">https:\/\/github.com\/johanneslumpe\/react-native-fs<\/a><\/p>\n<h2>\u5b89\u88c5\u5f15\u7528<\/h2>\n<p><!-- more --><\/p>\n<p>\u5b89\u88c5\u7ec4\u4ef6<\/p>\n<pre><code>npm install react-native-fs --save<\/code><\/pre>\n<p>\u5b89\u88c5\u4f9d\u8d56<\/p>\n<pre><code>rnpm link<\/code><\/pre>\n<blockquote>\n<p>\u6ce8\uff1a\u8be5\u547d\u4ee4\u9700\u8981\u91cd\u65b0\u7f16\u8bd1\u9879\u76ee\uff0c\u5e76\u4e14\u9996\u5148\u9700\u8981\u5b89\u88c5 rmpm<\/p>\n<\/blockquote>\n<pre><code>npm install rnpm -g<\/code><\/pre>\n<p>Android\uff1a\u6ce8\u518c\u6a21\u5757\uff08\u9700\u91cd\u65b0\u7f16\u8bd1\uff09<\/p>\n<pre><code class=\"language-java\">@Override\nprotected List&lt;ReactPackage&gt; getPackages() {\n    return Arrays.&lt;ReactPackage&gt;asList(\n        new MainReactPackage(), new RNFSPackage()\n    );\n}<\/code><\/pre>\n<p>iOS\uff1a\u624b\u52a8\u6dfb\u52a0\u5e93<\/p>\n<p>node_modules\/react-native-fs\/react-native-fs\/RNFS.xcodeproj<\/p>\n<h2>API \u4ecb\u7ecd<\/h2>\n<h3>\u5e38\u91cf<\/h3>\n<ul>\n<li><code>MainBundlePath<\/code> (String)\uff1aThe absolute path to the main bundle directory<\/li>\n<li><code>CachesDirectoryPath<\/code> (String)\uff1aThe absolute path to the caches directory<\/li>\n<li><code>DocumentDirectoryPath<\/code> (String)\uff1aThe absolute path to the document directory<\/li>\n<li><code>TemporaryDirectoryPath<\/code> (String)\uff1aThe absolute path to the temporary directory (iOS only)<\/li>\n<li><code>ExternalDirectoryPath<\/code> (String)\uff1aThe absolute path to the external files, shared directory (android only)<\/li>\n<li><code>ExternalStorageDirectoryPath<\/code> (String)\uff1aThe absolute path to the external storage, shared directory (android only)<\/li>\n<\/ul>\n<p>Android \u4e0b\u8f93\u51fa\uff08\u5c0f\u7c734\uff09\uff1a<\/p>\n<pre><code>\u4e3b\u8981bundle\u76ee\u5f55\uff1aundefined\n\u7f13\u5b58\u76ee\u5f55\uff1a\/data\/user\/0\/com.awesomeproject\/cache\n\u6587\u6863\u76ee\u5f55\uff1a\/data\/user\/0\/com.awesomeproject\/files\n\u4e34\u65f6\u76ee\u5f55iOS\uff1anull\n\u5916\u90e8\u5b58\u50a8\u76ee\u5f55Android\uff1a\/storage\/emulated\/0\/Android\/data\/com.awesomeproject\/files\n\u56fe\u7247\u76ee\u5f55\uff1a\/storage\/emulated\/0\/Pictures<\/code><\/pre>\n<h3>\u65b9\u6cd5<\/h3>\n<p>\uff081\uff09<code>readDir(dirpath: string): Promise&lt;ReadDirItem[]&gt;<\/code><\/p>\n<p>\u8bfb\u53d6\u76ee\u5f55\u5185\u5bb9\uff0c\u8fd4\u56de<\/p>\n<pre><code class=\"language-javascript\">type ReadDirItem = {\n  name: string;     \/\/ The name of the item\n  path: string;     \/\/ The absolute path to the item\n  size: string;     \/\/ Size in bytes\n  isFile: () =&gt; boolean;        \/\/ Is the file just a file?\n  isDirectory: () =&gt; boolean;   \/\/ Is the file a directory?\n};<\/code><\/pre>\n<p>\uff082\uff09<code>readdir(dirpath: string): Promise&lt;string[]&gt;<\/code><\/p>\n<p>\u8bfb\u53d6\u76ee\u5f55\u5185\u5bb9\uff0c\u8fd4\u56de<\/p>\n<p>\uff083\uff09<code>stat(filepath: string): Promise&lt;StatResult&gt;<\/code><\/p>\n<p>\u7edf\u8ba1\u6587\u4ef6\u4fe1\u606f\uff0c\u8fd4\u56de\u6587\u4ef6\u5c5e\u6027<\/p>\n<pre><code class=\"language-javascript\">type StatResult = {\n  name: string;     \/\/ The name of the item\n  path: string;     \/\/ The absolute path to the item\n  size: string;     \/\/ Size in bytes\n  mode: number;     \/\/ UNIX file mode\n  isFile: () =&gt; boolean;        \/\/ Is the file just a file?\n  isDirectory: () =&gt; boolean;   \/\/ Is the file a directory?\n};<\/code><\/pre>\n<p>\uff084\uff09<code>readFile(filepath: string, encoding?: string): Promise&lt;string&gt;<\/code><\/p>\n<p>\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff0c\u7f16\u7801\u53ef\u4e3a\uff1autf8 (default), ascii, base64<\/p>\n<p>\uff085\uff09<code>writeFile(filepath: string, contents: string, encoding?: string): Promise&lt;void&gt;<\/code><\/p>\n<p>\u5199\u5165\u6587\u4ef6\u5185\u5bb9\uff0c\u7f16\u7801\u53ef\u4e3a\uff1autf8 (default), ascii, base64<\/p>\n<p>\uff086\uff09<code>appendFile(filepath: string, contents: string, encoding?: string): Promise&lt;void&gt;<\/code><\/p>\n<p>\u5199\u5165\u6587\u4ef6\u5185\u5bb9\uff0c\u7f16\u7801\u53ef\u4e3a\uff1autf8 (default), ascii, base64<\/p>\n<p>\uff087\uff09<code>moveFile(filepath: string, destPath: string): Promise&lt;void&gt;<\/code><\/p>\n<p>\u79fb\u52a8\u6587\u4ef6<\/p>\n<p>\uff088\uff09<code>copyFile(filepath: string, destPath: string): Promise&lt;void&gt;<\/code><\/p>\n<p>\u590d\u5236\u6587\u4ef6<\/p>\n<p>\uff089\uff09<code>unlink(filepath: string): Promise&lt;void&gt;<\/code><\/p>\n<p>\u5220\u9664\u6587\u4ef6<\/p>\n<p>\uff0810\uff09<code>exists(filepath: string): Promise&lt;boolean&gt;<\/code><\/p>\n<p>\u5224\u65ad\u6587\u4ef6\u662f\u5426\u5b58\u5728<\/p>\n<p>\uff0811\uff09<code>mkdir(filepath: string, options?: MkdirOptions): Promise&lt;void&gt;<\/code><\/p>\n<p>\u521b\u5efa\u76ee\u5f55\uff0c\u8be5\u547d\u4ee4\u4f1a\u81ea\u52a8\u521b\u5efa\u7236\u76ee\u5f55\uff0c\u5e76\u4e14\u5728\u76ee\u5f55\u5df2\u5b58\u5728\u7684\u60c5\u51b5\u4e0b\u4e5f\u4e0d\u4f1a\u629b\u51fa\u5f02\u5e38<\/p>\n<p>MkdirOptions \u53ea\u5728 iOS \u5e73\u53f0\u9700\u8981\u8bbe\u7f6e\uff1a<\/p>\n<pre><code class=\"language-javascript\">type MkdirOptions = {\n    NSURLIsExcludedFromBackupKey?: boolean; \/\/ iOS only\n};<\/code><\/pre>\n<p>\uff0812\uff09<code>downloadFile(options: DownloadFileOptions): { jobId: number, promise: Promise&lt;DownloadResult&gt; }<\/code><\/p>\n<p>\u4e0b\u8f7d\u6587\u4ef6\uff0c\u9002\u914d Android &amp; iOS \u5e73\u53f0<\/p>\n<p>\u4f20\u5165\u53c2\u6570\uff1a<\/p>\n<pre><code class=\"language-javascript\">type DownloadFileOptions = {\n  fromUrl: string;          \/\/ URL to download file from\n  toFile: string;           \/\/ Local filesystem path to save the file to\n  headers?: Headers;        \/\/ An object of headers to be passed to the server\n  background?: boolean;\n  progressDivider?: number;\n  begin?: (res: DownloadBeginCallbackResult) =&gt; void;\n  progress?: (res: DownloadProgressCallbackResult) =&gt; void;\n};<\/code><\/pre>\n<p>\u8fd4\u56de\u53c2\u6570\uff1a<\/p>\n<pre><code class=\"language-javascript\">type DownloadResult = {\n  jobId: number;          \/\/ The download job ID, required if one wishes to cancel the download. See `stopDownload`.\n  statusCode: number;     \/\/ The HTTP status code\n  bytesWritten: number;   \/\/ The number of bytes written to the file\n};<\/code><\/pre>\n<p>\u4e0b\u8f7d\u94fe\u63a5<code>options.fromUrl<\/code>\u7684\u5185\u5bb9\u5230\u6587\u4ef6<code>options.toFile<\/code>\u4e2d\uff0c\u5c06\u4f1a\u8986\u76d6\u5df2\u5b58\u5728\u7684\u6587\u4ef6\u3002<\/p>\n<ul>\n<li><code>options.begin?<\/code><\/li>\n<\/ul>\n<p>\u4e0b\u8f7d\u6587\u4ef6\u5f00\u59cb\u7684\u56de\u8c03\u51fd\u6570\uff0c\u53ef\u7528\u4e8e\u83b7\u53d6\u4e00\u4e9b\u521d\u59cb\u5316\u4fe1\u606f\uff0c\u5982\u4efb\u52a1id\u3001\u5185\u5bb9\u957f\u5ea6\u7b49\u3002<\/p>\n<pre><code class=\"language-javascript\">type DownloadBeginCallbackResult = {\n  jobId: number;          \/\/ The download job ID, required if one wishes to cancel the download. See `stopDownload`.\n  statusCode: number;     \/\/ The HTTP status code\n  contentLength: number;  \/\/ The total size in bytes of the download resource\n  headers: Headers;       \/\/ The HTTP response headers from the server\n};<\/code><\/pre>\n<ul>\n<li><code>options.progress?<\/code><\/li>\n<\/ul>\n<p>\u4e0b\u8f7d\u8fc7\u7a0b\u56de\u8c03\u51fd\u6570\uff0c\u53ef\u7528\u4e8e\u83b7\u53d6\u5b9e\u65f6\u4e0b\u8f7d\u8fdb\u5ea6\u4fe1\u606f\u3002<\/p>\n<pre><code class=\"language-javascript\">type DownloadProgressCallbackResult = {\n  jobId: number;          \/\/ The download job ID, required if one wishes to cancel the download. See `stopDownload`.\n  contentLength: number;  \/\/ The total size in bytes of the download resource\n  bytesWritten: number;   \/\/ The number of bytes written to the file so far\n};<\/code><\/pre>\n<ul>\n<li><code>options.progressDivider<\/code><\/li>\n<\/ul>\n<p>\u4e0b\u8f7d\u8fc7\u7a0b progressCallback \u56de\u8c03\u9891\u7387\uff0c\u9ed8\u8ba4\u503c\u4e3a 0<\/p>\n<p>\u5982\u679c progressDivider = 10\uff0c\u5c06\u53ea\u63a5\u653610\u6b21 progressCallback \u56de\u8c03\uff1a0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100\uff1b\u5982\u679c progressDivider = 0\uff0c\u5c06\u63a5\u6536\u6240\u6709 progressCallback \u56de\u8c03\u3002<\/p>\n<ul>\n<li><code>options.background<\/code><\/li>\n<\/ul>\n<p>\u53ea\u5728 iOS \u5e73\u53f0\u6709\u6548\uff0c\u9ed8\u8ba4\u503c\u4e3a false<\/p>\n<p>\u5f53 APP \u5931\u53bb\u7126\u70b9\u65f6\u662f\u5426\u540e\u53f0\u7ee7\u7eed\u4e0b\u8f7d<\/p>\n<p>\uff0813\uff09<code>stopDownload(jobId: number): Promise&lt;void&gt;<\/code><\/p>\n<p>\u505c\u6b62\u4e0b\u8f7d\u6587\u4ef6<\/p>\n<p>\uff0814\uff09<code>uploadFiles(options: UploadFileOptions): { jobId: number, promise: Promise&lt;UploadResult&gt; }<\/code><\/p>\n<p>\u4e0a\u4f20\u6587\u4ef6\uff0c\u53ea\u9002\u7528\u4e8e iOS \u5e73\u53f0<\/p>\n<p>\u4f20\u5165\u53c2\u6570\uff1a<\/p>\n<pre><code class=\"language-javascript\">type UploadFileOptions = {\n  toUrl: string;            \/\/ URL to upload file to\n  files: UploadFileItem[];  \/\/ An array of objects with the file information to be uploaded.\n  headers?: Headers;        \/\/ An object of headers to be passed to the server\n  fields?: Fields;          \/\/ An object of fields to be passed to the server\n  method?: string;          \/\/ Default is &#039;POST&#039;, supports &#039;POST&#039; and &#039;PUT&#039;\n  begin?: (res: UploadBeginCallbackResult) =&gt; void;\n  progress?: (res: UploadProgressCallbackResult) =&gt; void;\n};<\/code><\/pre>\n<p>\u6587\u4ef6\u7ed3\u6784\u5bf9\u8c61 UploadFileItem\uff1a<\/p>\n<pre><code class=\"language-javascript\">type UploadFileItem = {\n  name: string;       \/\/ Name of the file, if not defined then filename is used\n  filename: string;   \/\/ Name of file\n  filepath: string;   \/\/ Path to file\n  filetype: string;   \/\/ The mimetype of the file to be uploaded, if not defined it will get mimetype from `filepath` extension\n};<\/code><\/pre>\n<p>\u8fd4\u56de\u53c2\u6570\uff1a<\/p>\n<pre><code class=\"language-javascript\">type UploadResult = {\n  jobId: number;        \/\/ The upload job ID, required if one wishes to cancel the upload. See `stopUpload`.\n  statusCode: number;   \/\/ The HTTP status code\n  headers: Headers;     \/\/ The HTTP response headers from the server\n  body: string;         \/\/ The HTTP response body\n};<\/code><\/pre>\n<ul>\n<li><code>options.begin<\/code><\/li>\n<\/ul>\n<p>\u4e0a\u4f20\u6587\u4ef6\u5f00\u59cb\u7684\u56de\u8c03\u51fd\u6570\uff0c\u53ef\u7528\u4e8e\u83b7\u53d6\u4e00\u4e9b\u521d\u59cb\u5316\u4fe1\u606f\uff0c\u5982\u4efb\u52a1id<\/p>\n<pre><code class=\"language-javascript\">type UploadBeginCallbackResult = {\n  jobId: number;        \/\/ The upload job ID, required if one wishes to cancel the upload. See `stopUpload`.\n};<\/code><\/pre>\n<ul>\n<li><code>options.progress<\/code><\/li>\n<\/ul>\n<p>\u4e0a\u4f20\u8fc7\u7a0b\u56de\u8c03\u51fd\u6570\uff0c\u53ef\u7528\u4e8e\u83b7\u53d6\u5b9e\u65f6\u4e0a\u4f20\u8fdb\u5ea6\u4fe1\u606f\u3002<\/p>\n<pre><code class=\"language-javascript\">type UploadProgressCallbackResult = {\n  jobId: number;                      \/\/ The upload job ID, required if one wishes to cancel the upload. See `stopUpload`.\n  totalBytesExpectedToSend: number;   \/\/ The total number of bytes that will be sent to the server\n  totalBytesSent: number;             \/\/ The number of bytes sent to the server\n};<\/code><\/pre>\n<p>\uff0815\uff09<code>stopUpload(jobId: number): Promise&lt;void&gt;<\/code><\/p>\n<p>\u505c\u6b62\u4e0a\u4f20\u6587\u4ef6\uff0c\u53ea\u9002\u7528\u4e8e iOS \u5e73\u53f0<\/p>\n<p>\uff0816\uff09<code>getFSInfo(): Promise&lt;FSInfoResult&gt;<\/code><\/p>\n<p>\u8fd4\u56de\u5b58\u50a8\u7a7a\u95f4\u4fe1\u606f<\/p>\n<pre><code class=\"language-javascript\">type FSInfoResult = {\n  totalSpace: number;   \/\/ The total amount of storage space on the device (in bytes).\n  freeSpace: number;    \/\/ The amount of available storage space on the device (in bytes).\n};<\/code><\/pre>\n<h3>\u8303\u4f8b\u6e90\u7801<\/h3>\n<p>index.android.js &amp; index.ios.js<\/p>\n<pre><code class=\"language-javascript\">\/**\n * Sample React Native App\n * https:\/\/github.com\/facebook\/react-native\n * @flow\n *\/\n\nimport React, { Component } from &#039;react&#039;;\nimport {\n    AppRegistry,\n    StyleSheet,\n    Text,\n    View\n} from &#039;react-native&#039;;\n\n\/\/ let RNFS = require(&#039;react-native-fs&#039;);\nimport RNFS from &#039;react-native-fs&#039;;\n\nlet downloadUrl = &#039;http:\/\/dldir1.qq.com\/qqfile\/qq\/QQ8.6\/18804\/QQ8.6.exe&#039;;\nlet jobId1 = -1, jobId2 = -1;\nlet testImage1Path = RNFS.PicturesDirectoryPath + &#039;\/test-image-11.png&#039;;\n\nclass FsDemo extends Component {\n    constructor(props) {\n        super(props);\/\/\u8fd9\u4e00\u53e5\u4e0d\u80fd\u7701\u7565\uff0c\u7167\u6284\u5373\u53ef\n            this.state = {\n            output:  &#039;\u6587\u6863\u76ee\u5f55\u8def\u5f84: &#039; + RNFS.DocumentDirectoryPath,\n        };\n    }\n\n    render() {\n        return (\n            &lt;View style={styles.container}&gt;\n                &lt;Text style={styles.welcome}&gt;\n                    React Native RNFS \u6587\u4ef6\u7ba1\u7406\n                &lt;\/Text&gt;\n                &lt;Text style={styles.text}&gt;{this.state.output}&lt;\/Text&gt;\n\n                &lt;Text style={{ fontSize: 25, marginTop: 5, marginBottom: 5 }} onPress={this.basic}&gt;\u5217\u51fa\u6587\u4ef6&lt;\/Text&gt;\n                &lt;Text style={{ fontSize: 25, marginTop: 5, marginBottom: 5 }} onPress={this.createFile}&gt;\u521b\u5efa\u6587\u4ef6&lt;\/Text&gt;\n                &lt;Text style={{ fontSize: 25, marginTop: 5, marginBottom: 5 }} onPress={this.delFile}&gt;\u5220\u9664\u6587\u4ef6&lt;\/Text&gt;\n                &lt;Text style={{ fontSize: 25, marginTop: 5, marginBottom: 5 }} onPress={this.printPath}&gt;\u8f93\u51fa\u5404\u79cd\u8def\u5f84&lt;\/Text&gt;\n                &lt;Text style={{ fontSize: 25, marginTop: 5, marginBottom: 5 }} onPress={this.downloadFile.bind(this, true, downloadUrl) }&gt;\u4e0b\u8f7d\u6587\u4ef6&lt;\/Text&gt;\n                &lt;Text style={{ fontSize: 25, marginTop: 5, marginBottom: 5 }} onPress={this.stopDownload}&gt;\u505c\u6b62\u4e0b\u8f7d&lt;\/Text&gt;\n                &lt;Text style={{ fontSize: 25, marginTop: 5, marginBottom: 5 }} onPress={this.getFileInfo}&gt;\u83b7\u53d6\u6587\u4ef6\u5927\u5c0f\u4fe1\u606f&lt;\/Text&gt;\n                &lt;Text style={{ fontSize: 25, marginTop: 5, marginBottom: 5 }} onPress={this.uploadFile}&gt;\u4e0a\u4f20\u6587\u4ef6iOS&lt;\/Text&gt;\n                &lt;Text style={{ fontSize: 25, marginTop: 5, marginBottom: 5 }} onPress={this.stopUpload}&gt;\u505c\u6b62\u4e0a\u4f20iOS&lt;\/Text&gt;\n\n                &lt;Text style={styles.author}&gt;Powered by RNAPP.CC&lt;\/Text&gt;\n            &lt;\/View&gt;\n        );\n    }\n\n    basic = () =&gt; {\n        \/\/ get a list of files and directories\n        RNFS.readDir(RNFS.PicturesDirectoryPath).then(\n            (result) =&gt; {\n                console.log(&#039;GOT RESULT&#039;, result);\n                \/\/ stat the first file\n                return Promise.all([RNFS.stat(result[0].path), result[0].path]);\n            })\n        .then(\n            (statResult) =&gt; {\n                if (statResult[0].isFile()) {\n                    \/\/ if we have a file, read it\n                    return RNFS.readFile(statResult[1], &#039;utf8&#039;);\n                }\n                return &#039;no file&#039;;\n            })\n        .then((contents) =&gt; {\n            \/\/ log the file contents\n            console.log(&#039;\u6587\u4ef6\u5185\u5bb9\uff1a&#039; + contents);\n        })\n        .catch((err) =&gt; {\n            console.log(&#039;\u9519\u8bef\uff1a&#039; + err.message);\n            console.log(err.message, err.code);\n        });\n    }\n\n    downloadFile(background, url) {\n        var progress = data =&gt; {\n            var percentage = ((100 * data.bytesWritten) \/ data.contentLength) | 0;\n            var text = `\u8fdb\u5ea6 ${percentage}%`;\n            this.setState({\n                output: text\n            });\n        };\n\n        var begin = res =&gt; {\n            jobId1 = res.jobId;\n        };\n\n        var progressDivider = 1;\n\n        RNFS.downloadFile(\n        {\n            fromUrl: url,\n            toFile: testImage1Path,\n            begin,\n            progress,\n            background,\n            progressDivider\n        }).promise.then(\n            res =&gt; {this.setState({\n                output: JSON.stringify(res)\n            });\n        }).catch(\n            err =&gt; this.showError(err)\n        );\n    }\n\n    stopDownload = () =&gt; {\n        console.log(&#039;\u505c\u6b62\u4e0b\u8f7d&#039;);\n        RNFS.stopDownload(jobId1);\n    }\n\n    showError = (err) =&gt; {\n        console.log(err.message);\n    }\n\n    createFile = () =&gt; {\n        \/\/ create a path you want to write to\n        let path = RNFS.PicturesDirectoryPath + &#039;\/test.txt&#039;;\n\n        \/\/ write the file\n        RNFS.writeFile(path, &#039;Lorem ipsum dolor sit amet&#039;, &#039;utf8&#039;)\n            .then((success) =&gt; {\n                console.log(&#039;FILE WRITTEN! \u76ee\u5f55\u662f: &#039; + path);\n            })\n            .catch((err) =&gt; {\n                console.log(err.message);\n            });\n    }\n\n    delFile = () =&gt; {\n        \/\/ create a path you want to delete\n        let path = RNFS.PicturesDirectoryPath + &#039;\/test.txt&#039;;\n\n        return RNFS.unlink(path)\n            .then(() =&gt; {\n                console.log(&#039;FILE DELETED&#039;);\n            })\n            \/\/ `unlink` will throw an error, if the item to unlink does not exist\n            .catch((err) =&gt; {\n                console.log(err.message);\n            });\n    }\n\n    uploadFile = () =&gt; {\n        \/\/ For testing purposes, go to http:\/\/requestb.in\/ and create your own link\n        var uploadUrl = &#039;http:\/\/requestb.in\/1mhbfei1&#039;;\n        \/\/ create an array of objects of the files you want to upload\n        var files = [\n            {\n                name: &#039;test1&#039;,\n                filename: &#039;test1.w4a&#039;,\n                filepath: RNFS.PicturesDirectoryPath + &#039;\/test1.w4a&#039;,\n                filetype: &#039;audio\/x-m4a&#039;\n            }, {\n                name: &#039;test2&#039;,\n                filename: &#039;test2.w4a&#039;,\n                filepath: RNFS.PicturesDirectoryPath + &#039;\/test2.w4a&#039;,\n                filetype: &#039;audio\/x-m4a&#039;\n            }\n        ];\n\n        var uploadBegin = (response) =&gt; {\n            jobId2 = response.jobId;\n            console.log(&#039;UPLOAD HAS BEGUN! JobId: &#039; + jobId2);\n        };\n\n        var uploadProgress = (response) =&gt; {\n            var percentage = Math.floor((response.totalBytesSent \/ response.totalBytesExpectedToSend) * 100);\n            console.log(&#039;UPLOAD IS &#039; + percentage + &#039;% DONE!&#039;);\n        };\n\n        \/\/ upload files\n        RNFS.uploadFiles(\n            {\n                toUrl: uploadUrl,\n                files: files,\n                method: &#039;POST&#039;,\n                headers: {\n                    &#039;Accept&#039;: &#039;application\/json&#039;,\n                },\n                fields: {\n                    &#039;hello&#039;: &#039;world&#039;,\n                },\n                begin: uploadBegin,\n                progress: uploadProgress\n            }\n            ).promise.then((response) =&gt; {\n                if (response.statusCode == 200) {\n                    console.log(&#039;FILES UPLOADED!&#039;); \/\/ response.statusCode, response.headers, response.body\n                } else {\n                    console.log(&#039;SERVER ERROR&#039;);\n                }\n            })\n            .catch((err) =&gt; {\n                if (err.description === &quot;cancelled&quot;) {\n                    \/\/ cancelled by user\n                }\n                console.log(err);\n            });\n    }\n\n    stopUpload = () =&gt; {\n        console.log(&#039;\u505c\u6b62\u4e0a\u4f20&#039;);\n    }\n\n    printPath = () =&gt; {\n        console.log(&#039;\u4e3b\u8981bundle\u76ee\u5f55\uff1a&#039; + RNFS.MainBundlePath); \/\/undefined\n        console.log(&#039;\u7f13\u5b58\u76ee\u5f55\uff1a&#039; + RNFS.CachesDirectoryPath);\n        console.log(&#039;\u6587\u6863\u76ee\u5f55\uff1a&#039; + RNFS.DocumentDirectoryPath);\n        console.log(&#039;\u4e34\u65f6\u76ee\u5f55iOS\uff1a&#039; + RNFS.TemporaryDirectoryPath); \/\/null\n        console.log(&#039;\u5916\u90e8\u5b58\u50a8\u76ee\u5f55Android\uff1a&#039; + RNFS.ExternalDirectoryPath);\n        console.log(&#039;\u56fe\u7247\u76ee\u5f55\uff1a&#039; + RNFS.PicturesDirectoryPath);\n    }\n\n    getFileInfo = () =&gt; {\n        return RNFS.getFSInfo().then(info =&gt; {\n            this.setState({\n                output: JSON.stringify(info)\n            });\n        });\n    }\n}\n\nconst styles = StyleSheet.create({\n    container: {\n        flex: 1,\n        justifyContent: &#039;center&#039;,\n        alignItems: &#039;center&#039;,\n        backgroundColor: &#039;#F5FCFF&#039;,\n    },\n    flex: {\n        flex: 1,\n    },\n    welcome: {\n        fontSize: 20,\n        textAlign: &#039;center&#039;,\n        margin: 10,\n    },\n    instructions: {\n        textAlign: &#039;center&#039;,\n        color: &#039;#333333&#039;,\n        marginBottom: 5,\n    },\n    text: {\n        fontSize: 16,\n        textAlign: &#039;center&#039;,\n        margin: 10,\n        color:&#039;red&#039;,\n    },\n    author: {\n        justifyContent: &#039;flex-end&#039;,\n        alignItems: &#039;center&#039;,\n        marginBottom: 10,\n        marginTop: 50,\n        textAlign: &#039;center&#039;,\n        fontSize: 16,\n        color: &#039;#3BC1FF&#039;,\n    },\n});\n\nAppRegistry.registerComponent(&#039;AwesomeProject&#039;, () =&gt; FsDemo);<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u53ef\u4ee5\u5b9e\u73b0\u6587\u4ef6\u7684\u521b\u5efa\u3001\u5220\u9664\u3001\u590d\u5236\u3001\u79fb\u52a8\u3001\u4e0b\u8f7d\u3001\u4e0a\u4f20\u7b49\u529f\u80fd\u3002 \u9879\u76ee\u5730\u5740\uff1ahttps:\/\/github.com\/jo [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[163],"tags":[],"class_list":["post-527","post","type-post","status-publish","format-standard","hentry","category-react-native"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/527","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=527"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/527\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=527"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=527"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=527"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}