Browse Source

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

Ben Hsieh 8 years ago
parent
commit
113e07ae08

+ 3
- 0
src/android/src/main/java/com/RNFetchBlob/RNFetchBlobConst.java View File

14
 	public static final MediaType MIME_ENCODED = MediaType.parse("application/x-www-form-urlencoded");
14
 	public static final MediaType MIME_ENCODED = MediaType.parse("application/x-www-form-urlencoded");
15
     public static final String FILE_PREFIX_BUNDLE_ASSET = "bundle-assets://";
15
     public static final String FILE_PREFIX_BUNDLE_ASSET = "bundle-assets://";
16
     public static final String FILE_PREFIX_CONTENT = "content://";
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 View File

79
                     if(!dir.exists())
79
                     if(!dir.exists())
80
                         dir.mkdirs();
80
                         dir.mkdirs();
81
                     FileOutputStream fout = new FileOutputStream(f, append);
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
                     fout.close();
101
                     fout.close();
84
                     promise.resolve(Arguments.createArray());
102
                     promise.resolve(Arguments.createArray());
85
                 } catch (Exception e) {
103
                 } catch (Exception e) {

+ 1
- 0
src/index.js View File

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