瀏覽代碼

Add Android file-to-file data access implementation #56

Ben Hsieh 8 年之前
父節點
當前提交
113e07ae08

+ 3
- 0
src/android/src/main/java/com/RNFetchBlob/RNFetchBlobConst.java 查看文件

@@ -14,4 +14,7 @@ public class RNFetchBlobConst {
14 14
 	public static final MediaType MIME_ENCODED = MediaType.parse("application/x-www-form-urlencoded");
15 15
     public static final String FILE_PREFIX_BUNDLE_ASSET = "bundle-assets://";
16 16
     public static final String FILE_PREFIX_CONTENT = "content://";
17
+    public static final String DATA_ENCODE_URI = "uri";
18
+    public static final String DATA_ENCODE_BASE64 = "base64";
19
+    public static final String DATA_ENCODE_UTF8 = "utf8";
17 20
 }

+ 19
- 1
src/android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java 查看文件

@@ -79,7 +79,25 @@ public class RNFetchBlobFS {
79 79
                     if(!dir.exists())
80 80
                         dir.mkdirs();
81 81
                     FileOutputStream fout = new FileOutputStream(f, append);
82
-                    fout.write(stringToBytes(data, encoding));
82
+                    // write data from a file
83
+                    if(encoding.equalsIgnoreCase(RNFetchBlobConst.DATA_ENCODE_URI)) {
84
+                        File src = new File(data);
85
+                        if(!src.exists()) {
86
+                            promise.reject("RNfetchBlob writeFileError", "source file : " + data + "not exists");
87
+                            fout.close();
88
+                            return null;
89
+                        }
90
+                        FileInputStream fin = new FileInputStream(src);
91
+                        byte [] buffer = new byte [10240];
92
+                        int read = fin.read(buffer);
93
+                        while(read > 0) {
94
+                            fout.write(buffer, 0, read);
95
+                            read = fin.read(buffer);
96
+                        }
97
+                        fin.close();
98
+                    }
99
+                    else
100
+                        fout.write(stringToBytes(data, encoding));
83 101
                     fout.close();
84 102
                     promise.resolve(Arguments.createArray());
85 103
                 } catch (Exception e) {

+ 1
- 0
src/index.js 查看文件

@@ -35,6 +35,7 @@ const {
35 35
 } = fs
36 36
 import polyfill from './polyfill'
37 37
 
38
+const Blob = polyfill.Blob
38 39
 const emitter = DeviceEventEmitter
39 40
 const RNFetchBlob:RNFetchBlobNative = NativeModules.RNFetchBlob
40 41