Skip to content
This repository has been archived by the owner on Mar 16, 2019. It is now read-only.

Commit

Permalink
Add Android file-to-file data access implementation #56
Browse files Browse the repository at this point in the history
  • Loading branch information
wkh237 committed Jul 21, 2016
1 parent dbc87d4 commit 113e07a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@ public class RNFetchBlobConst {
public static final MediaType MIME_ENCODED = MediaType.parse("application/x-www-form-urlencoded");
public static final String FILE_PREFIX_BUNDLE_ASSET = "bundle-assets://";
public static final String FILE_PREFIX_CONTENT = "content://";
public static final String DATA_ENCODE_URI = "uri";
public static final String DATA_ENCODE_BASE64 = "base64";
public static final String DATA_ENCODE_UTF8 = "utf8";
}
20 changes: 19 additions & 1 deletion src/android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,25 @@ protected Integer doInBackground(String... args) {
if(!dir.exists())
dir.mkdirs();
FileOutputStream fout = new FileOutputStream(f, append);
fout.write(stringToBytes(data, encoding));
// write data from a file
if(encoding.equalsIgnoreCase(RNFetchBlobConst.DATA_ENCODE_URI)) {
File src = new File(data);
if(!src.exists()) {
promise.reject("RNfetchBlob writeFileError", "source file : " + data + "not exists");
fout.close();
return null;
}
FileInputStream fin = new FileInputStream(src);
byte [] buffer = new byte [10240];
int read = fin.read(buffer);
while(read > 0) {
fout.write(buffer, 0, read);
read = fin.read(buffer);
}
fin.close();
}
else
fout.write(stringToBytes(data, encoding));
fout.close();
promise.resolve(Arguments.createArray());
} catch (Exception e) {
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const {
} = fs
import polyfill from './polyfill'

const Blob = polyfill.Blob
const emitter = DeviceEventEmitter
const RNFetchBlob:RNFetchBlobNative = NativeModules.RNFetchBlob

Expand Down

0 comments on commit 113e07a

Please sign in to comment.